Back to Browse

Asteroid Collision | Stack Simulation | JavaScript | LeetCode 735

27 views
Apr 26, 2026
10:55

πŸ“ Description: πŸ”₯ Day 94 of my LeetCode Daily Challenge! In this problem, I solve LeetCode 735 - Asteroid Collision using a Stack-based simulation in JavaScript. This is a classic problem where we simulate collisions between asteroids moving in a straight line. Each asteroid has a direction: Positive β†’ moving right ➑️ Negative β†’ moving left ⬅️ When two asteroids meet, they collide based on their sizes. 🧠 Key Idea: We use a stack to keep track of asteroids that are still alive. When the current asteroid is moving left (less than 0) And the top of the stack is moving right (greater than 0) πŸ‘‰ A collision happens ⚑ Collision Logic: Let’s compare: top of stack vs current asteroid Cases: If one is bigger β†’ smaller one gets destroyed If both are equal β†’ both get destroyed If current survives β†’ keep checking with previous stack elements πŸš€ Approach: Iterate through all asteroids Use a stack to simulate collisions Resolve collisions using a while loop Push asteroid only if it survives Return the final stack βœ… Complexity: Time Complexity: O(n) Space Complexity: O(n) πŸ“š In this video, you will learn: How to simulate collisions using a stack How to handle multiple collisions in a loop Why stack is the right data structure here How to manage direction-based conditions A common pattern in simulation problems πŸ”— Problem Link: https://leetcode.com/problems/asteroid-collision/ If you found this helpful, don’t forget to like, comment, and subscribe for more daily LeetCode solutions, JavaScript DSA problems, and coding interview prep πŸš€ #leetcode #javascript #dsa #day94 #asteroidcollision #LeetCode735 #stack #simulation #arrays #codinginterview #algorithms #datastructures #problemsolving #leetcodejavascript #100daysofcode

Download

1 formats

Video Formats

360pmp47.1 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Asteroid Collision | Stack Simulation | JavaScript | LeetCode 735 | NatokHD