Skip to content

Commit c056834

Browse files
committed
Crane_claw_machine_game / 중급
1 parent 4ff7262 commit c056834

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function solution(board, moves) {
2+
// 2차원 배열 board를 각 열마다 스택 생성 5x5일 경우 총 5개의 스택 생성
3+
let stack = [...Array(board[0].length)].map(() => []);
4+
5+
for (let i = board.length - 1; i >= 0; i--) {
6+
for (let j = 0; j < board[0].length; j++) {
7+
if (board[i][j] !== 0) {
8+
// 0이 아닐때만 각 스택에 추가
9+
stack[j].push(board[i][j]); // 스택의 각 열에 맞춰 board의 요소들 추가
10+
}
11+
}
12+
}
13+
14+
let removeCount = 0;
15+
let bucket = [];
16+
17+
for (let m of moves) {
18+
if (stack[m - 1].length > 0) {
19+
// 스택의 열에 인형이 존재하면
20+
const doll = stack[m - 1].pop(); // 맨 위의 인형 집어서
21+
// 바구니에 인형이 있고 맨 위의 인형이 지금 넣으려는 인형과 같으면
22+
if (bucket.length > 0 && bucket[bucket.length - 1] === doll) {
23+
bucket.pop(); // 바구니 맨 위의 인형 제거
24+
removeCount += 2; // 제거한 갯수 2개 추가
25+
} else {
26+
bucket.push(doll); // 바구니에 인형이 없거나 맨 위 인형과 같지 않은 인형이면 추가
27+
}
28+
}
29+
}
30+
31+
return removeCount;
32+
}

0 commit comments

Comments
 (0)