# Solution For Candy Crush

I’m sorry, but as an AI language model, I am not able to browse web pages nor return a solution to the LeetCode problem as I am not able to access it. However, as a general approach to solving problems on LeetCode, here are some steps that might help:

Read the problem carefully: This is the first and most important step. Understanding the problem statement, the input format, the output format, and the constraints is crucial.

Break down the problem: Try to break the problem down into smaller sub-problems. Identify the core problem that needs solving and define the necessary functions and data structures that will make the problem-solving process easier.

Write pseudo code: Write the solution in pseudo code. This will help you understand the problem better and guide you toward the solution.

Write code and test: Write the code for each function defined in the pseudo code, and test each function until you are confident it works as expected.

Optimize: Look for ways to optimize the solution. Consider edge cases and the time and space complexity of the solution.

Submit: Finally, submit the solution to LeetCode, and see if it passes all the test cases.

Remember, solving problems on LeetCode requires practice and patience. Keep practicing and learning, and you’ll get better at it over time.

## Step by Step Implementation For Candy Crush

This is a Java solution for the Leetcode problem "Candy Crush". /* Given a 2D integer array board representing the game board, and an integer score representing the current score of the game, return the maximum score that can be achieved by crushing candies. */ public int candyCrush(int[][] board, int score) { // Base case if (board.length == 0 || board[0].length == 0) { return score; } int R = board.length; int C = board[0].length; // Variable to keep track of whether the board has been changed or not boolean hasChanged = false; // Loop through the board for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { // Check if the current cell is a candy if (board[r][c] != 0) { // Check if there are 3 or more candies in a row/column int horizLength = 1; int vertLength = 1; // Check horizontally for (int i = c + 1; i < C; i++) { if (board[r][i] == board[r][c]) { horizLength++; } else { break; } } // Check vertically for (int i = r + 1; i < R; i++) { if (board[i][c] == board[r][c]) { vertLength++; } else { break; } } // If there are 3 or more candies in a row/column, crush them and update the score if (horizLength >= 3 || vertLength >= 3) { hasChanged = true; score += board[r][c]; // Crush horizontally for (int i = c; i < c + horizLength; i++) { board[r][i] = 0; } // Crush vertically for (int i = r; i < r + vertLength; i++) { board[i][c] = 0; } } } } } // If the board has been changed, crush candies recursively if (hasChanged) { return candyCrush(board, score); } // Otherwise, return the current score return score; }

This is a classic game of matching 3 or more identical items in a grid. The game ends when there are no more moves left. The input is a 2D array of integers, where 1 represents a candy and 0 represents an empty space. The output should be a 2D array of the same size, where 1 represents a candy and 0 represents an empty space. def candy_crush(grid): R = len(grid) C = len(grid[0]) to_crush = set() # scan rows for r in range(R): for c in range(C-2): if grid[r][c] == grid[r][c+1] == grid[r][c+2] != 0: to_crush |= {(r,c), (r,c+1), (r,c+2)} # scan cols for c in range(C): for r in range(R-2): if grid[r][c] == grid[r+1][c] == grid[r+2][c] != 0: to_crush |= {(r,c), (r+1,c), (r+2,c)} # crush for r, c in to_crush: grid[r][c] = 0 # gravity for c in range(C): write = R-1 for read in reversed(range(R)): if grid[read][c] != 0: grid[write][c] = grid[read][c] write -= 1 for write in range(write+1): grid[write][c] = 0 return grid

var candyCrush = function(board) { // Your code here };

This is a classic game-theory problem. The key is to find the stable configuration (i.e. the configuration that cannot be improved further by making a move). Consider a row of n candies, with the ith candy having value ai. We can perform the following move: choose any three candies in a row, and remove them, getting points equal to the product of their values. We want to maximize the number of points we get. It is easy to see that the optimal strategy is to always choose the triplet of candies with the highest product. Thus, the problem can be reduced to finding the maximum product of three numbers in an array. This can be done in O(n) time using a single pass of the array.

// This is a C# solution to the LeetCode problem "Candy Crush" // https://leetcode.com/problems/candy-crush/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LeetCode { class Program { static void Main(string[] args) { // Create a new instance of the CandyCrush class CandyCrush cc = new CandyCrush(); // Initialize the game board int[][] board = new int[][] { new int[] { 110,5,112,113,114 }, new int[] { 210,211,5,213,214 }, new int[] { 310,311,3,313,314 }, new int[] { 410,411,412,5,414 }, new int[] { 5,1,512,3,3 }, new int[] { 610,4,1,613,614 }, new int[] { 710,1,2,713,714 }, new int[] { 810,1,2,1,1 }, new int[] { 1,1,2,2,2 }, new int[] { 4,1,4,4,1014 } }; // Print the initial state of the game board Console.WriteLine("Initial state:"); PrintBoard(board); // Crush the candies and print the state of the board after each move Console.WriteLine(" After candy crush:"); for (int i = 0; i < board.Length; i++) { for (int j = 0; j < board[0].Length; j++) { int val = board[i][j]; if (val > 0) { int row = i; int col = j; while (row + 1 < board.Length && Math.Abs(board[row + 1][col] - val) == 1) { row++; } while (col + 1 < board[0].Length && Math.Abs(board[row][col + 1] - val) == 1) { col++; } if (row != i || col != j) { for (int r = i; r <= row; r++) { for (int c = j; c <= col; c++) { board[r][c] = 0; } } } } } PrintBoard(board); } // Prints the state of the game board to the console static void PrintBoard(int[][] board) { for (int i = 0; i < board.Length; i++) { for (int j = 0; j < board[0].Length; j++) { Console.Write(board[i][j] + "\t"); } Console.WriteLine(); } } } }