We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c34eecd commit 1f6fd0bCopy full SHA for 1f6fd0b
unn04012/stack/Crane_Machine_Game.js
@@ -0,0 +1,43 @@
1
+function solution(board, moves) {
2
+ let answer = 0;
3
+ const stack = [];
4
+ const width = board.length;
5
+
6
+ const pullCrane = (order) => {
7
+ const w = order - 1;
8
+ let h = 0;
9
10
+ while (width > h) {
11
+ const doll = board[h][w];
12
+ if (doll !== 0) {
13
+ if (stack[stack.length - 1] === doll) {
14
+ stack.pop();
15
+ answer += 2;
16
+ } else {
17
+ stack.push(board[h][w]);
18
+ }
19
20
+ board[h][w] = 0;
21
+ break;
22
23
+ h++;
24
25
+ };
26
27
+ moves.forEach((e) => pullCrane(e));
28
29
+ return answer;
30
+}
31
32
+console.log(
33
+ solution(
34
+ [
35
+ [0, 0, 0, 0, 0],
36
+ [0, 0, 1, 0, 3],
37
+ [0, 2, 5, 0, 1],
38
+ [4, 2, 4, 4, 2],
39
+ [3, 5, 1, 3, 1],
40
+ ],
41
+ [1, 5, 3, 5, 1, 2, 1, 4]
42
+ )
43
+);
0 commit comments