Skip to content

Commit ca265f5

Browse files
authored
Update 723-candy-crush.js
1 parent 2c20f5d commit ca265f5

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

723-candy-crush.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,58 @@ const inflictGravity = function (board) {
8282
}
8383
}
8484
}
85+
86+
// another
87+
88+
/**
89+
* @param {number[][]} board
90+
* @return {number[][]}
91+
*/
92+
const candyCrush = function (board) {
93+
const N = board.length,
94+
M = board[0].length
95+
let found = true
96+
while (found) {
97+
found = false
98+
for (let i = 0; i < N; i++) {
99+
for (let j = 0; j < M; j++) {
100+
const val = Math.abs(board[i][j])
101+
if (val === 0) continue
102+
if (
103+
j < M - 2 &&
104+
Math.abs(board[i][j + 1]) == val &&
105+
Math.abs(board[i][j + 2]) == val
106+
) {
107+
found = true
108+
let ind = j
109+
while (ind < M && Math.abs(board[i][ind]) == val)
110+
board[i][ind++] = -val
111+
}
112+
if (
113+
i < N - 2 &&
114+
Math.abs(board[i + 1][j]) == val &&
115+
Math.abs(board[i + 2][j]) == val
116+
) {
117+
found = true
118+
let ind = i
119+
while (ind < N && Math.abs(board[ind][j]) == val)
120+
board[ind++][j] = -val
121+
}
122+
}
123+
}
124+
if (found) {
125+
// move positive values to the bottom, then set the rest to 0
126+
for (let j = 0; j < M; j++) {
127+
let storeInd = N - 1
128+
for (let i = N - 1; i >= 0; i--) {
129+
if (board[i][j] > 0) {
130+
board[storeInd--][j] = board[i][j]
131+
}
132+
}
133+
for (let k = storeInd; k >= 0; k--) board[k][j] = 0
134+
}
135+
}
136+
}
137+
return board
138+
}
139+

0 commit comments

Comments
 (0)