LeetCode Solutions
723. Candy Crush
Time: $O(m^2n^2)$ Space: $O(1)$
class Solution {
public:
vector<vector<int>> candyCrush(vector<vector<int>>& board) {
const int m = board.size();
const int n = board[0].size();
bool haveCrushes = true;
while (haveCrushes) {
haveCrushes = false;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
const int val = abs(board[i][j]);
if (val == 0)
continue;
// Crush vertical candies
if (j + 2 < n && abs(board[i][j + 1]) == val &&
abs(board[i][j + 2]) == val) {
haveCrushes = true;
for (int k = j; k < j + 3; ++k)
board[i][k] = -val;
}
// Crush horizontal candies
if (i + 2 < m && abs(board[i + 1][j]) == val &&
abs(board[i + 2][j]) == val) {
haveCrushes = true;
for (int k = i; k < i + 3; ++k)
board[k][j] = -val;
}
}
if (haveCrushes) {
// For each column, drop candies
for (int j = 0; j < n; ++j) {
int nextIndex = m - 1;
for (int i = m - 1; i >= 0; --i)
if (board[i][j] > 0)
board[nextIndex--][j] = board[i][j];
// Set board[0..nextIndex][j] to 0s
for (int i = nextIndex; i >= 0; --i)
board[i][j] = 0;
}
}
}
return board;
}
};
class Solution {
public int[][] candyCrush(int[][] board) {
final int m = board.length;
final int n = board[0].length;
boolean haveCrushes = true;
while (haveCrushes) {
haveCrushes = false;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
final int val = Math.abs(board[i][j]);
if (val == 0)
continue;
// Crush vertical candies
if (j + 2 < n && Math.abs(board[i][j + 1]) == val && Math.abs(board[i][j + 2]) == val) {
haveCrushes = true;
for (int k = j; k < j + 3; ++k)
board[i][k] = -val;
}
// Crush horizontal candies
if (i + 2 < m && Math.abs(board[i + 1][j]) == val && Math.abs(board[i + 2][j]) == val) {
haveCrushes = true;
for (int k = i; k < i + 3; ++k)
board[k][j] = -val;
}
}
if (haveCrushes) {
// For each column, drop candies
for (int j = 0; j < n; ++j) {
int nextIndex = m - 1;
for (int i = m - 1; i >= 0; --i)
if (board[i][j] > 0)
board[nextIndex--][j] = board[i][j];
// Set board[0..nextIndex][j] to 0s
for (int i = nextIndex; i >= 0; --i)
board[i][j] = 0;
}
}
}
return board;
}
}