코딩응급실
프로그래머스: 택배상자 본문
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
}
}
'Java' 카테고리의 다른 글
프로그래머스: [1차] 프렌즈4블록 (0) | 2024.03.26 |
---|---|
프로그래머스: 숫자 변환 (0) | 2024.03.26 |
프로그래머스: [3차]파일명 정렬 (0) | 2024.03.21 |
프로그래머스: 오픈채팅방 (0) | 2024.03.20 |
프로그래머스: 롤케이크 자르기 (0) | 2024.03.20 |