Skip to content

Commit 7f0fada

Browse files
committed
Claw machine / 중급
1 parent 4f2458f commit 7f0fada

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const solution = (boards, moves) => {
2+
let stack = [];
3+
let removedCnt = 0;
4+
for (let i = 0; i < moves.length; i++) {
5+
let pickedToy = -1;
6+
let col = moves[i] - 1; // 격자의 열 선택(크레인 위치)
7+
for (let row = 0; row < boards.length; row++) {
8+
// 아래로 크레인 내리기
9+
if (boards[row][col] !== 0) {
10+
pickedToy = boards[row][col];
11+
boards[row][col] = 0;
12+
break;
13+
}
14+
}
15+
if (pickedToy === -1) continue; // 인형이 집히지 않은 경우, 아래 스킵
16+
if (pickedToy === stack[stack.length - 1]) {
17+
stack.pop();
18+
removedCnt += 2;
19+
} else {
20+
stack.push(pickedToy);
21+
}
22+
}
23+
return removedCnt;
24+
};

0 commit comments

Comments
 (0)