Asteroid Collision - Complete Solution Guide
Asteroid Collision is LeetCode problem 735, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
We are given an array asteroids of integers representing asteroids in a row. The indices of the asteriod in the array represent their relative position in space. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explo
Detailed Explanation
The Asteroid Collision problem simulates the collision of asteroids moving in a single line. Each asteroid is represented by an integer, where the absolute value is its size and the sign represents its direction (positive: right, negative: left). The task is to determine the final state of the asteroids after all collisions have occurred. Asteroids moving in the same direction will never collide. When asteroids collide, the smaller one explodes. If they are the same size, both explode. The input is an array of integers, and the output is an array representing the remaining asteroids after all collisions.
Solution Approach
The solution uses a stack to keep track of the asteroids that have not yet collided. It iterates through the input array. If an asteroid is moving to the right (positive), it's pushed onto the stack. If an asteroid is moving to the left (negative), it checks for collisions with the asteroids on top of the stack (those moving to the right). If a collision occurs, the smaller asteroid is destroyed. If they are the same size, both are destroyed. The process continues until the stack is empty or the negative asteroid is destroyed or encounters a larger positive asteroid. If the negative asteroid survives, it's pushed onto the stack.
Step-by-Step Algorithm
- Step 1: Initialize an empty stack to store the surviving asteroids.
- Step 2: Iterate through the input array `asteroids`.
- Step 3: If the current asteroid is positive (moving right), push it onto the stack.
- Step 4: If the current asteroid is negative (moving left), enter a `while` loop that continues as long as the stack is not empty and the top of the stack is a positive asteroid (moving right).
- Step 5: Inside the `while` loop, compare the absolute value of the negative asteroid with the top of the stack:
- - If the top of the stack is smaller, pop it from the stack (it explodes).
- - If the top of the stack is equal, pop it from the stack (both explode), and break out of the `while` loop.
- - If the top of the stack is larger, break out of the `while` loop (the negative asteroid explodes).
- Step 6: If the `while` loop completes without the negative asteroid exploding (i.e., the stack is empty or the top of the stack is negative or 0), push the negative asteroid onto the stack.
- Step 7: After processing all asteroids, convert the stack to an array and return it.
- Step 8: In C specifically, remember to allocate memory for the return array and free the stack's memory.
Key Insights
- Insight 1: The core idea is to use a stack to simulate the collision process. Positive asteroids are pushed onto the stack, representing asteroids moving to the right.
- Insight 2: When a negative asteroid (moving left) encounters positive asteroids in the stack (moving right), we need to simulate the collisions until the stack is empty, the negative asteroid is destroyed, or a larger positive asteroid is encountered.
- Insight 3: The edge case where all asteroids are moving in the same direction or there are no collisions can be handled directly by using the stack to collect surviving asteroids.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Stack, Simulation.
Companies
Asked at: Accolite, Citadel, Deutsche Bank, DoorDash, Dream11, EPAM Systems, Flipkart, IMC, Lyft, Myntra, OpenAI, PhonePe, Qualtrics, Roku, Salesforce, ServiceNow, SoFi, Sprinklr, Zoho, Zynga.