Skip to content

Commit

Permalink
CT_280
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade-Good committed Oct 13, 2024
1 parent 5842167 commit 7bb3d0a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions PGM/Java/src/lv2/택배상자.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lv2;

import java.util.Stack;

public class 택배상자 {
public int solution(int[] order) {

Stack<Integer> stack = new Stack<>();
int idx = 0;
int answer = 0;

for (int i = 1; i <= order.length; i++) { // 기존 컨테이너의 순서

// 보조 컨테이너 확인
while (!stack.empty() && idx < order.length && stack.peek() == order[idx]) {
stack.pop();
idx++;
answer++;
}

// 기존 컨테이너 확인
if (idx == order.length) {
return answer;
}

if (order[idx] != i) { // 다름
stack.push(i);
} else { // 찾음
answer++;
idx++;
}
}

// 보조 컨테이너 확인
while (!stack.empty() && idx < order.length && stack.peek() == order[idx]) {
stack.pop();
idx++;
answer++;
}

return answer;
}
}

0 comments on commit 7bb3d0a

Please sign in to comment.