Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 택배상자 본문

Java

프로그래머스: 택배상자

Daeryuk Kim 2024. 3. 26. 22:20
import java.util.*;

class Solution {
    public int solution(int[] order) {
        int answer = 0;
        Stack<Integer> stack = new Stack<>();
        int index = 0;
        for (int i = 1; i <= order.length; i++) {
            stack.push(i);
            while (!stack.isEmpty() && stack.peek() == order[index]) {
                stack.pop();
                index++;
                answer++;
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        int[] order = {4,3,1,2,5};
        int result = sol.solution(order);
        System.out.println(result); // 출력: 2
    }
}