forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy Crush.java
More file actions
42 lines (37 loc) · 1.38 KB
/
Candy Crush.java
File metadata and controls
42 lines (37 loc) · 1.38 KB
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
class Solution {
public int[][] candyCrush(int[][] board) {
int rows = board.length;
int cols = board[0].length;
boolean flag = false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j + 2 < cols; j++) {
int val = Math.abs(board[i][j]);
if (val != 0 && val == Math.abs(board[i][j + 1]) && val == Math.abs(board[i][j + 2])) {
board[i][j] = board[i][j + 1] = board[i][j + 2] = -val;
flag = true;
}
}
}
for (int i = 0; i + 2 < rows; i++) {
for (int j = 0; j < cols; j++) {
int val = Math.abs(board[i][j]);
if (val != 0 && val == Math.abs(board[i + 1][j]) && val == Math.abs(board[i + 2][j])) {
board[i][j] = board[i + 1][j] = board[i + 2][j] = -val;
flag = true;
}
}
}
for (int i = 0; i < cols; ++i) {
int rightRow = rows - 1;
for (int j = rows - 1; j >= 0; --j) {
if (board[j][i] > 0) {
board[rightRow--][i] = board[j][i];
}
}
while (rightRow >= 0) {
board[rightRow--][i] = 0;
}
}
return flag ? candyCrush(board) : board;
}
}