613. Asteroid Collision

MediumStackStackSimulation

Given asteroids in a row, each value's sign is its direction (positive = right, negative = left) and its magnitude is size. Asteroids moving toward each other collide; the smaller explodes, and equal sizes both explode. Same-direction asteroids never meet. Return the state after all collisions. The input is JSON {asteroids}.

Input: JSON {asteroids}.

Output: Array — the surviving asteroids.

Examples

Example 1
Input: {"asteroids":[5,10,-5]}
Output: [5,10]
Explanation: 10 destroys -5.
Example 2
Input: {"asteroids":[8,-8]}
Output: []
Explanation: Equal sizes both explode.
Example 3
Input: {"asteroids":[10,2,-5]}
Output: [10]
Explanation: -5 destroys 2 then loses to 10.

Constraints

Asked by

OracleAmazonFlipkartDeloitteMicrosoftIBM
Solve this problem in the editor →