KatInterview

723. Candy Crush

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Simulation with In-place Marking

This problem asks us to simulate the candy crushing process on a game board until it becomes stable. The process involves two main steps repeated in a loop: identifying and crushing candies, followed by applying gravity to let remaining candies fall.

Solution

Candy Crush in Python

class Solution:
    def candyCrush(self, board: list[list[int]]) -> list[list[int]]:
        R, C = len(board), len(board[0])
        todo = True

        while todo:
            todo = False

            # Mark candies to be crushed
            # Mark horizontal crushes
            for r in range(R):
                for c in range(C - 2):
                    v = abs(board[r][c])
                    if v != 0 and v == abs(board[r][c+1]) and v == abs(board[r][c+2]):
                        board[r][c] = board[r][c+1] = board[r][c+2] = -v
                        todo = True
            
            # Mark vertical crushes
            for r in range(R - 2):
                for c in range(C):
                    v = abs(board[r][c])
                    if v != 0 and v == abs(board[r+1][c]) and v == abs(board[r+2][c]):
                        board[r][c] = board[r+1][c] = board[r+2][c] = -v
                        todo = True
            
            # If candies were crushed, apply gravity
            if todo:
                for c in range(C):
                    wr = R - 1  # write row pointer
                    for r in range(R - 1, -1, -1):
                        if board[r][c] > 0:
                            board[wr][c] = board[r][c]
                            wr -= 1
                    # Fill the rest of the column with 0s
                    for r in range(wr, -1, -1):
                        board[r][c] = 0
        
        return board

Candy Crush in Java

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

        while (todo) {
            todo = false;

            // Mark candies to be crushed
            // Mark horizontal crushes
            for (int r = 0; r < R; r++) {
                for (int c = 0; c < C - 2; c++) {
                    int v = Math.abs(board[r][c]);
                    if (v != 0 && v == Math.abs(board[r][c+1]) && v == Math.abs(board[r][c+2])) {
                        board[r][c] = board[r][c+1] = board[r][c+2] = -v;
                        todo = true;
                    }
                }
            }

            // Mark vertical crushes
            for (int r = 0; r < R - 2; r++) {
                for (int c = 0; c < C; c++) {
                    int v = Math.abs(board[r][c]);
                    if (v != 0 && v == Math.abs(board[r+1][c]) && v == Math.abs(board[r+2][c])) {
                        board[r][c] = board[r+1][c] = board[r+2][c] = -v;
                        todo = true;
                    }
                }
            }

            // If candies were crushed, apply gravity
            if (todo) {
                for (int c = 0; c < C; c++) {
                    int wr = R - 1; // write row pointer
                    for (int r = R - 1; r >= 0; r--) {
                        if (board[r][c] > 0) {
                            board[wr--][c] = board[r][c];
                        }
                    }
                    // Fill the rest of the column with 0s
                    while (wr >= 0) {
                        board[wr--][c] = 0;
                    }
                }
            }
        }

        return board;
    }
}

Candy Crush in C++

class Solution {
public:
    vector<vector<int>> candyCrush(vector<vector<int>>& board) {
        int R = board.size(), C = board[0].size();
        bool todo = true;

        while (todo) {
            todo = false;

            // Mark candies to be crushed
            for (int r = 0; r < R; ++r) {
                for (int c = 0; c < C - 2; ++c) {
                    int v = abs(board[r][c]);
                    if (v != 0 && v == abs(board[r][c+1]) && v == abs(board[r][c+2])) {
                        board[r][c] = board[r][c+1] = board[r][c+2] = -v;
                        todo = true;
                    }
                }
            }

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

            // If candies were crushed, apply gravity
            if (todo) {
                for (int c = 0; c < C; ++c) {
                    int wr = R - 1; // write row pointer
                    for (int r = R - 1; r >= 0; --r) {
                        if (board[r][c] > 0) {
                            board[wr--][c] = board[r][c];
                        }
                    }
                    // Fill the rest of the column with 0s
                    while (wr >= 0) {
                        board[wr--][c] = 0;
                    }
                }
            }
        }
        return board;
    }
};

Candy Crush in TypeScript

function candyCrush(board: number[][]): number[][] {
    const R = board.length;
    const C = board[0].length;
    let todo = true;

    while (todo) {
        todo = false;

        // Mark candies to be crushed
        // Mark horizontal crushes
        for (let r = 0; r < R; r++) {
            for (let c = 0; c < C - 2; c++) {
                const v = Math.abs(board[r][c]);
                if (v !== 0 && v === Math.abs(board[r][c+1]) && v === Math.abs(board[r][c+2])) {
                    board[r][c] = board[r][c+1] = board[r][c+2] = -v;
                    todo = true;
                }
            }
        }

        // Mark vertical crushes
        for (let r = 0; r < R - 2; r++) {
            for (let c = 0; c < C; c++) {
                const v = Math.abs(board[r][c]);
                if (v !== 0 && v === Math.abs(board[r+1][c]) && v === Math.abs(board[r+2][c])) {
                    board[r][c] = board[r+1][c] = board[r+2][c] = -v;
                    todo = true;
                }
            }
        }

        // If candies were crushed, apply gravity
        if (todo) {
            for (let c = 0; c < C; c++) {
                let wr = R - 1; // write row pointer
                for (let r = R - 1; r >= 0; r--) {
                    if (board[r][c] > 0) {
                        board[wr--][c] = board[r][c];
                    }
                }
                // Fill the rest of the column with 0s
                while (wr >= 0) {
                    board[wr--][c] = 0;
                }
            }
        }
    }

    return board;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.