Cars sit on a straight rail. cars[i] is a non-zero integer: its absolute value is the car's weight and its sign is its direction (positive moves right, negative moves left). All cars move at the same speed.
When two cars meet, the lighter one is knocked off the rail; if they weigh the same both are knocked off. Two cars moving in the same direction never meet. Return the list of cars that remain, in their original order.
Examples
Input: cars = [-2, 4, 6, -5, 1]
Output: [-2, 4, 6, 1]
Explanation: 6 and -5 collide; -5 is knocked off. -2 drives away to the left and 1 to the right, so they meet nobody.
Input: cars = [3, 7, -7]
Output: [3]
Explanation: 7 and -7 weigh the same, so both are knocked off.
Input: cars = [-3, 9, 1, 4, -6]
Output: [-3, 9]
Explanation: -6 knocks off 4, then 1, and is then knocked off by 9; -3 drives away to the left.
Constraints
cars[i] != 0- Target:
O(n)time
Goals
- Only a right-moving car on the stack can collide with a new left-moving car
- Resolve a chain of collisions with a while loop against the stack top