Skip to content

Commit 7e659a4

Browse files
committed
Parcel box / 심화
1 parent e2458e1 commit 7e659a4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function solution(order) {
2+
const stack = [];
3+
let idx = 0; // 처리해야되는 order의 인덱스
4+
let cnt = 0;
5+
6+
for (let currentBox = 1; currentBox <= order.length; currentBox++) {
7+
if (currentBox === order[idx]) {
8+
// 처리가능하면 처리하고, 스택도 비울만큼 비우기
9+
cnt++;
10+
idx++;
11+
while (stack.length > 0 && stack.at(-1) === order[idx]) {
12+
stack.pop();
13+
cnt++;
14+
idx++;
15+
}
16+
} else {
17+
// 처리못하면 스택에 보관
18+
stack.push(currentBox);
19+
}
20+
}
21+
22+
while (stack.length > 0 && stack.at(-1) === order[idx]) {
23+
// 스택에 남은 것들 처리하기
24+
stack.pop();
25+
cnt++;
26+
idx++;
27+
}
28+
29+
return cnt;
30+
}

0 commit comments

Comments
 (0)