Skip to main content

Command Palette

Search for a command to run...

Asteroid Collision

Updated
2 min read
Asteroid Collision
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

We are given an array asteroids of integers representing asteroids in a row.

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 explode. Two asteroids moving in the same direction will never meet.

LeetCode Problem - 735

class Solution {

    // Method to simulate asteroid collisions and return the final state of the asteroids
    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack = new Stack<>();  // Stack to keep track of surviving asteroids

        // Iterate through each asteroid in the array
        for (int asteroid : asteroids) {
            boolean destroyed = false;  // Flag to track if the current asteroid is destroyed

            // Handle the case where there is a potential collision: 
            // The current asteroid is moving left (asteroid < 0), and the top of the stack is moving right (stack.peek() > 0)
            while (!stack.isEmpty() && asteroid < 0 && stack.peek() > 0) {

                // If the absolute value of the current asteroid is greater, it destroys the top of the stack
                if (stack.peek() < -asteroid) {
                    stack.pop();  // Pop the top of the stack (right-moving asteroid is destroyed)

                // If the absolute values of the asteroids are equal, both are destroyed
                } else if (stack.peek() == -asteroid) {
                    stack.pop();  // Pop the top of the stack (both are destroyed)
                    destroyed = true;  // Mark the current asteroid as destroyed
                    break;  // Exit the loop as no further collisions can happen

                // If the right-moving asteroid is larger, the current asteroid is destroyed
                } else {
                    destroyed = true;  // Mark the current asteroid as destroyed
                    break;  // Exit the loop as no further collisions can happen
                }
            }

            // If the current asteroid was not destroyed, push it onto the stack
            if (!destroyed) {
                stack.push(asteroid);
            }
        }

        // Convert the stack of surviving asteroids to an array and return the result
        return convertListToArray(stack);
    }

    // Helper method to convert a stack of asteroids to an array
    public int[] convertListToArray(Stack<Integer> arrList) {
        int[] result = new int[arrList.size()];  // Create an array with the size of the stack
        for (int i = arrList.size() - 1; i >= 0; i--) {
            result[i] = arrList.pop();  // Pop each element from the stack into the array
        }
        return result;  // Return the array of surviving asteroids
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.

Asteroid Collision