Convoy Collisions
Carts on a single track are listed left to right in carts. The absolute value is a cart's mass and the sign its direction: positive moves right, negative moves left. All carts move at the same speed.
When two carts meet, the lighter one is destroyed; if they weigh the same, both are destroyed. Carts moving in the same direction never meet. Return the surviving carts in left-to-right order.
Example 1
Input: carts = [5, 10, -5] Output: [5, 10] Explanation: 10 and -5 meet and 10 survives.
Example 2
Input: carts = [8, -8] Output: []
Constraints
- 0 <= carts.length <= 10^5
- 1 <= |carts[i]| <= 1000
Hint 1
Only a left-moving cart can hit the right-moving carts already on the stack.
Hint 2
Keep popping while the incoming cart is heavier than the top.
Discussion 0
Join free to post in the discussion.
Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Game Developer
Run checks your code on 2 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.