Skip to content

Commit cec3d58

Browse files
committed
105차 3번 문제풀이
1 parent 84d966e commit cec3d58

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
function solution(order) {
2-
const containerBelt = [];
3-
let now = 1;
2+
let box = 1;
43
let count = 0;
4+
let tempBox = [];
5+
let i = 0;
56

6-
for (let i = 0; i < order.length; i++) {
7-
const target = order[i];
8-
9-
console.log(target);
10-
11-
while (now <= order.length && now !== target) {
12-
containerBelt.push(now);
13-
now++;
7+
while (box <= order.length) {
8+
if (order[i] === box) {
9+
count++;
10+
i++;
11+
box++;
12+
} else if (tempBox.length > 0 && tempBox[tempBox.length - 1] === order[i]) {
13+
tempBox.pop();
14+
count++;
15+
i++;
16+
} else {
17+
tempBox.push(box);
18+
box++;
1419
}
15-
console.log("belt:", containerBelt);
20+
}
1621

17-
if (now === target) {
18-
count++;
19-
now++;
20-
} else if (containerBelt.length && containerBelt[0] === target) {
21-
containerBelt.shift();
22+
while (tempBox.length > 0) {
23+
if (tempBox[tempBox.length - 1] === order[i]) {
24+
tempBox.pop();
2225
count++;
26+
i++;
2327
} else {
2428
break;
2529
}
26-
27-
console.log("--------------");
2830
}
2931

3032
return count;
3133
}
3234

33-
console.log(solution([4, 3, 1, 2, 5])); // 출력: 2
35+
console.log(solution([4, 3, 1, 2, 5]));

0 commit comments

Comments
 (0)