Skip to content

Commit ee8476f

Browse files
committed
크레인 인형뽑기 / 중급
1 parent 488872b commit ee8476f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function solution(my_string) {
2+
let arr = my_string.split(' ');
3+
let result = Number(arr[0]);
4+
5+
for(let i = 1; i < arr.length; i += 2) {
6+
const sign = arr[i];
7+
const num = Number(arr[i + 1]);
8+
9+
if(sign === '+') {
10+
result += num;
11+
}
12+
if(sign === '-') {
13+
result -= num;
14+
}
15+
}
16+
17+
return result;
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function solution(board, moves) {
2+
let answer = 0;
3+
let stack = [];
4+
5+
for(let move of moves) {
6+
// moves 배열의 값을 인덱스로 사용하기 위해 1을 빼줍니다
7+
let col = move - 1;
8+
9+
// 해당 열에서 가장 위에 있는 인형을 찾습니다
10+
for(let row = 0; row < board.length; row++) {
11+
if(board[row][col] !== 0) {
12+
let doll = board[row][col];
13+
14+
// 인형을 집었으니 해당 위치를 0으로 변경
15+
board[row][col] = 0;
16+
17+
// 스택의 최상단 인형과 같다면 두 인형을 터뜨립니다
18+
if(stack[stack.length - 1] === doll) {
19+
stack.pop();
20+
answer += 2;
21+
} else {
22+
stack.push(doll);
23+
}
24+
break;
25+
}
26+
}
27+
}
28+
29+
return answer;
30+
}

0 commit comments

Comments
 (0)