Beakjoon&프로그래머스/Java

[프로그래머스/Java] 배열 만들기 4

현장 2025. 2. 8. 21:28

-Code

import java.util.Stack;

class Solution {
    public int[] solution(int[] arr) {
        Stack<Integer> stk = new Stack<>();
        int idx = 0;

        while (idx < arr.length) {
            if (stk.isEmpty() || stk.get(stk.size() - 1) < arr[idx]) {
                stk.add(arr[idx]);
                idx++;
            } else {
                stk.pop();
            }
        }

        return stk.stream().mapToInt(Integer::intValue).toArray();
    }
}