Skip to content

Latest commit

 

History

History
141 lines (125 loc) · 4.29 KB

735. Asteroid Collision.md

File metadata and controls

141 lines (125 loc) · 4.29 KB

leetcode Daily Challenge on October 21th, 2020.


Difficulty : Medium

Related Topics : Stack


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.

Example 1:

Input: asteroids = [5,10,-5]
Output: [5,10]
Explanation: The 10 and -5 collide resulting in 10.  The 5 and 10 never collide.

Example 2:

Input: asteroids = [8,-8]
Output: []
Explanation: The 8 and -8 collide exploding each other.

Example 3:

Input: asteroids = [10,2,-5]
Output: [10]
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.

Example 4:

Input: asteroids = [-2,-1,1,2]
Output: [-2,-1,1,2]
Explanation: The -2 and -1 are moving left, while the 1 and 2 are moving right. Asteroids moving the same direction never meet, so no asteroids will meet each other.

Constraints:

  • 1 <= asteroids <= 10^4
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

Solution

  • mine
    • Java
      • Runtime: 4 ms, faster than 89.47%, Memory Usage: 39.7 MB, less than 9.99% of Java online submissions
        // O(N)time
        // O(N)space
        public int[] asteroidCollision(int[] asteroids) {
            LinkedList<Integer> list = new LinkedList<>();
            int IDLE = 1001;
            for(int i : asteroids){
                int now = i;
                while(!list.isEmpty() && list.getLast() > 0 && now < 0){
                    int last = list.removeLast();
                    if(Math.abs(last) > Math.abs(now)){
                        now = last;
                    }else if(last + now == 0){
                        if(list.isEmpty()){
                            now = IDLE;
                        }else{
                            now = list.removeLast();
                        }
                    }
                }
                if(now != IDLE) list.add(now);
            }
            int[] res = new int[list.size()];
            int i = 0;
            while(!list.isEmpty()) res[i++] = list.removeFirst();
            return res;
        }
        

  • the most votes
  • Runtime: 1 ms, faster than 100.00%, Memory Usage: 40 MB, less than 9.99% of Java online submissions
    // O(N)time
    // O(N)space
    public int[] asteroidCollision(int[] asteroids) {
        if (asteroids == null || asteroids.length == 0) {
            return null;
        }
    
        // two pointers
        // let end be a pointer such that asteroids[end] contains the asteroids that is safe for now
        // if asteroids[end] is negative and asteroids[i] is positive, then put i to end + 1, and update end to end + 1
        // if asteroids[end] and asteroids [i] are both negative or position, put i to end + 1 and update end to end + 1
        // if asteroids[end] is positive and asteroids[i] is negative,
        //    while (end >= 0 && asteroids[end] <= -asteroids[i]) end--
    
        int end = 0;
        for (int i = 1; i < asteroids.length; ++i) {
            if (asteroids[i] > 0) {
                asteroids[++end] = asteroids[i];
            } else if (asteroids[i] < 0) {
                boolean isExploded = false;
                while (end >= 0 && asteroids[end] > 0 && asteroids[end] <= -asteroids[i]) {
                    int temp = asteroids[end--];
                    if (temp == -asteroids[i]) {
                        isExploded = true;
                        break;
                    }
                }
                if ((end < 0 || asteroids[end] < 0) && !isExploded) {
                    asteroids[++end] = asteroids[i];
                }
            }
        }
        int[] res = new int[end + 1];
        for (int i = 0; i < end + 1; ++i) {
            res[i] = asteroids[i];
        }
        return res;
    }