Oh. My. Goodness. I was so excited to solve this Algo. The Grinch has initiated an attack on the North Pole! We need to take out his Missiles before they hit the workshop and destroy all the gifts!
Who doesn’t love a good holiday themed ANYTHING?? Even if it means the possible destruction of the North Pole!
Let’s get started!
Problem
Please click the link below to view the Problem Statement
Solution
I start out by creating a for of loop so that I can access the elements of the missiles object. Using dot notation, I add a new {key: value}, with impact as the key, and the value = to the missiles distance / missiles speed. By doing this, I know what time each missile is scheduled to hit.
Then, I sort the object by the impact time, loop through the new sorted object of missiles and pushed the missile name into our answer array.
As always, here is a faster, optimal, one liner solution! This is not my code! I’ve linked the programmer’s CodeWars solution below. Please check him out!
Conclusion
Thank you so much for stopping by! Stay tuned for more Algorithm Content!
Please checkout my online portfolio and feel free to connect with me on Linkedin!
Sources: