Given an array of integers
asteroids
representing the planets in the same row.For each element in the array, its absolute value represents the size of the planet, and its positive or negative represents the direction of movement of the planet (positive means moving to the right, negative means moving to the left). Every planet moves at the same speed.
Find all planets left after the collision. Collision rules: Two planets collide with each other, the smaller planet will explode. If two planets are the same size, both planets will explode. Two planets moving in the same direction will never collide.
Example 1:
Input: asteroids = [5,10,-5] Output: [5,10] Explanation: After 10 and -5 collide, only 10 remains. 5 and 10 never collide.Example 2:
Input: asteroids = [8,-8] Output: [] Explanation: After 8 and -8 collide, both explode.Example 3:
Input: asteroids = [10,2,-5] Output: [10] Explanation: 2 and -5 collide and -5 remains. 10 and -5 collide leaving 10 .
The topic comes from the feedback of the friends who finished the interview, thank you very much, old iron!