본문 바로가기

알고리즘/자료구조유형(알고리즘 문제)

leetcode[735] Asteroid Collision

https://leetcode.com/problems/asteroid-collision/

735. Asteroid Collision(행성 충돌)

<처음부터 짜긴 어려운 이상적 코드>

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.stream.Collectors;
 
public class Main {
    public static Deque<Integer> solve(int[] asteroids) {
        Deque<Integer> deque = new LinkedList<>();
        for(var ast: asteroids){
            boolean flag = true;
            while(!deque.isEmpty() && deque.peekLast() > 0 && ast < 0){
                int tmp = deque.peekLast() + ast;
                if(tmp <= 0) deque.removeLast(); // 작거나 같으면 오른쪽 행성 폭파
                if(tmp >= 0){ // 크거나 같으면 왼쪽 행성 폭파
                    flag = false;
                    break;
                }
            }
            if(flag) deque.addLast(ast);
        }
        return deque;
    }

    public static void main(String[] args) {
        try(
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in))
        ){
            int T = Integer.parseInt(in.readLine());
            for(int t = 0; t < T; ++t) {
                in.readLine();
                int[] asteroids = Arrays.stream(in.readLine().split(" "))
                    .mapToInt(Integer::parseInt).toArray();
                Deque<Integer> deque = solve(asteroids);
                if(deque.size() == 0) System.out.println();
                else System.out.println(deque.stream().map(n -> n.toString())
                    .collect(Collectors.joining(" ")));
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
 

<내가 짠 judge코드>

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public class Testing3 {
    public static Deque<Integer> solution(int[] arr) {
        int size = arr.length;
        Deque<Integer> answerList = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            int ele = arr[i];
            boolean flag = true;
            while (!answerList.isEmpty() && answerList.peekLast() > 0 && ele < 0) {
                int lastOne = answerList.peekLast() + ele;
                if (lastOne > 0) {
                    flag = false;
                    break;
                } else if (lastOne == 0) {
                    answerList.pollLast();
                    flag = false;
                    break;
                } else {
                    answerList.pollLast();
                }
            }
            if (flag)
                answerList.addLast(ele);
        }
        return answerList;
    }
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        for (int t = 0; t < T; ++t) {
            br.readLine();
            int[] arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            Deque<Integer> answer = solution(arr);
            if (answer.size() == 0)
                System.out.println();
            else
                System.out.println(answer.stream().map(m -> m.toString()).collect(Collectors.joining(" ")));
        }
    }
}

 

<Leetcode 제출 코드>

class Solution {
    public int[] asteroidCollision(int[] arr) {
           int size = arr.length;
        Deque<Integer> answerList = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            int ele = arr[i];
            boolean flag = true;
            while (!answerList.isEmpty() && answerList.peekLast() > 0 && ele < 0) {
                int lastOne = answerList.peekLast() + ele;
                if (lastOne > 0) {
                    flag = false;
                    break;
                } else if (lastOne == 0) {
                    answerList.pollLast();
                    flag = false;
                    break;
                } else {
                    answerList.pollLast();
                }
            }
            if (flag)
                answerList.addLast(ele);
        }
        return answerList.stream().mapToInt(Integer::valueOf).toArray();
    }
}