import java.util.Arrays;

/**
 * This question is about implementing a basic elimination algorithm for Candy Crush.
 *
 * Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent different types of candies. A value of board[i][j] = 0 represents that the cell at position (i, j) is empty. The given board represents the state of the game following the player's move. Now, you need to restore the board to a stable state by crushing candies according to the following rules:
 *
 * If three or more candies of the same type are adjacent vertically or horizontally, "crush" them all at the same time - these positions become empty.
 * After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, then these candies will drop until they hit a candy or bottom at the same time. (No new candies will drop outside the top boundary.)
 * After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.
 * If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board.
 * You need to perform the above rules until the board becomes stable, then return the current board.
 *
 *
 *
 * Example:
 *
 * Input:
 * board =
 * [[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]]
 *
 * Output:
 * [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]
 *
 * Explanation:
 *
 *
 *
 * Note:
 *
 * The length of board will be in the range [3, 50].
 * The length of board[i] will be in the range [3, 50].
 * Each board[i][j] will initially start as an integer in the range [1, 2000].
 *
 *
 * Approach: Split into four separate steps,
 *
 * 1. Identify streaks
 * 2. Delete streaks
 * 3. Adjust board
 * 4. Repeat if moves were made
 *
 * Time complxity: O(n ^ 2) * k (number of repetitions)
 * Memory complexity: O(n ^ 2)
 */
public class CandyCrush {
static final int LEFT = 0, UP = 1;
public int[][] candyCrush(int[][] board) {
    if (board.length == 0 || board[0].length == 0) return board;
    int [][][] streak = new int[board.length][board[0].length][2];
    int [] limits = new int[board[0].length];
    while (removeElements(board, streak, limits));
    return board;
}

boolean removeElements(int [][] board, int [][][] streak, int [] limits) {
    boolean moved = false;
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            int up = i - 1 >= 0 ? board[i - 1][j] : -1;
            int left = j - 1 >= 0 ? board[i][j - 1] : -1;
            streak[i][j][UP] = board[i][j] == up && up != 0 ? streak[i - 1][j][UP] + 1 : 1;
            streak[i][j][LEFT] = board[i][j] == left && left != 0 ? streak[i][j - 1][LEFT] + 1 : 1;
        }
    }
    for (int i = board.length - 1; i >= 0; i--) {
        for (int j = board[i].length - 1; j >= 0; j--) {
            int up = streak[i][j][UP];
            int left = streak[i][j][LEFT];
            if (up > 2) {
                moved = true;
                while (up > 0) {
                    up--;
                    board[i - up][j] = 0;
                    streak[i - up][j][UP] = 0;
                }
            }
            if (left > 2) {
                moved = true;
                while (left > 0) {
                    left--;
                    board[i][j - left] = 0;
                    streak[i][j - left][LEFT] = 0;
                }
            }
        }
    }
    for (int i = board.length - 1; i >= 0; i--) {
        for (int j = board[i].length - 1; j >= 0; j--) {
            if (board[i][j] != 0) {
                int k = board.length - 1 - limits[j];
                board[k][j] = board[i][j];
                if (k != i) {
                    board[i][j] = 0;
                }
                limits[j]++;
            }
            if (i == 0) limits[j] = 0;
        }
    }
    return moved;
}

    public void test() {
        int [][] board = candyCrush(new int[][]{
                {110,5,112,113,114},
                {210,211,5,213,214},
                {310,311,3,313,314},
                {410,411,412,5,414},
                {5,1,512,3,3},
                {610,4,1,613,614},
                {710,1,2,713,714},
                {810,1,2,1,1},
                {1,1,2,2,2},
                {4,1,4,4,1014}
        });
    }
    void printBoard(int [][] board) {
        for (int [] array : board) {
            System.out.println(Arrays.toString(array));
        }
        System.out.println();
    }
}
