forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
50 lines (50 loc) · 1.63 KB
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function candyCrush(board: number[][]): number[][] {
const m = board.length;
const n = board[0].length;
let run = true;
while (run) {
run = false;
for (let i = 0; i < m; i++) {
for (let j = 2; j < n; j++) {
if (
board[i][j] !== 0 &&
Math.abs(board[i][j]) === Math.abs(board[i][j - 1]) &&
Math.abs(board[i][j]) === Math.abs(board[i][j - 2])
) {
run = true;
const val = Math.abs(board[i][j]);
board[i][j] = board[i][j - 1] = board[i][j - 2] = -val;
}
}
}
for (let j = 0; j < n; j++) {
for (let i = 2; i < m; i++) {
if (
board[i][j] !== 0 &&
Math.abs(board[i][j]) === Math.abs(board[i - 1][j]) &&
Math.abs(board[i][j]) === Math.abs(board[i - 2][j])
) {
run = true;
const val = Math.abs(board[i][j]);
board[i][j] = board[i - 1][j] = board[i - 2][j] = -val;
}
}
}
if (run) {
for (let j = 0; j < n; j++) {
let k = m - 1;
for (let i = m - 1; i >= 0; i--) {
if (board[i][j] > 0) {
board[k][j] = board[i][j];
k--;
}
}
while (k >= 0) {
board[k][j] = 0;
k--;
}
}
}
}
return board;
}