슈콩

[프로그래머스] Lv.2 택배상자 본문

Algorithms/Programmers

[프로그래머스] Lv.2 택배상자

shukong 2025. 10. 16. 19:34

 

[문제]

https://school.programmers.co.kr/learn/courses/30/lessons/131704

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

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