[ 프로그래머스 ] #131704 : 택배상자 - JAVA

🔗택배상자

import java.util.*;
class Solution {
    public int solution(int[] order) {
        // 보조는 stack
        // 알려준 순서대로 택배 상자를 실어야 함 
        int boxes= order.length; // 박스 개수
        int orderBox=1; // 택시에 실어야하는 박스 번호
        int [] truck = new int [boxes+1];
        Stack <Integer> subTruck = new Stack<>();
        // order 와 안 같으면 보조에 다 넣기
        int answer=0;
        // 컨베이어 벨트 (1~n)
        for(int con=1;con<=boxes;con++){
            // 보조 확인하기 
            // order 값과 다르면 보조에 다 담기
            if(order[orderBox-1]!=con){
                subTruck.push(con);
            }
            // order 값과 같으면 트럭에 담기
            else{
                orderBox++; // 다음 주문으로
                answer++;
            }

            while(!subTruck.isEmpty() && subTruck.peek()==order[orderBox-1]){
                subTruck.pop();
                orderBox++; // 다음 주문으로
                answer++;
            }
        }
        return answer;
    }
}