LeetCode 723: Candy Crush Solution

Master LeetCode problem 723 (Candy Crush), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

723. Candy Crush

Problem Explanation

Explanation:

To solve this problem, we can use a brute-force approach where we iterate through the board to find and remove any contiguous groups of 3 or more same candies. We can use a flag to mark cells that need to be removed, then perform gravity to move candies down and fill in empty spaces.

  • Iterate through the board and mark cells with the same candies in vertical or horizontal directions.
  • If any cell is marked, set it to 0.
  • Perform gravity by moving candies down and fill in empty spaces.
  • Repeat the process until no more candies can be crushed.

Time Complexity:

The time complexity of this approach is O((R * C)^2), where R is the number of rows and C is the number of columns in the board.

Space Complexity:

The space complexity is O(1) as we are modifying the input board in place without using any additional data structures.

:

Solution Code

class Solution {
    public int[][] candyCrush(int[][] board) {
        int R = board.length, C = board[0].length;
        boolean found = true;

        while (found) {
            found = false;
            for (int r = 0; r < R; r++) {
                for (int c = 0; c < C - 2; c++) {
                    int val = Math.abs(board[r][c]);
                    if (val != 0 && val == Math.abs(board[r][c + 1]) && val == Math.abs(board[r][c + 2])) {
                        board[r][c] = board[r][c + 1] = board[r][c + 2] = -val;
                        found = true;
                    }
                }
            }

            for (int r = 0; r < R - 2; r++) {
                for (int c = 0; c < C; c++) {
                    int val = Math.abs(board[r][c]);
                    if (val != 0 && val == Math.abs(board[r + 1][c]) && val == Math.abs(board[r + 2][c])) {
                        board[r][c] = board[r + 1][c] = board[r + 2][c] = -val;
                        found = true;
                    }
                }
            }

            if (found) {
                for (int c = 0; c < C; c++) {
                    int idx = R - 1;
                    for (int r = R - 1; r >= 0; r--) {
                        if (board[r][c] > 0) {
                            board[idx--][c] = board[r][c];
                        }
                    }
                    while (idx >= 0) {
                        board[idx--][c] = 0;
                    }
                }
            }
        }

        return board;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 723 (Candy Crush)?

This page provides optimized solutions for LeetCode problem 723 (Candy Crush) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 723 (Candy Crush)?

The time complexity for LeetCode 723 (Candy Crush) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 723 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 723 in Java, C++, or Python.

Back to LeetCode Solutions