Candy Crush - Problem

Imagine you're playing the classic Candy Crush game! 🍭 You have an m x n grid filled with colorful candies, where each candy is represented by a positive integer. Empty cells are represented by 0.

Your task is to simulate the candy crushing mechanics until the board reaches a stable state. Here's how it works:

  1. 🔍 Find Matches: Identify groups of 3 or more identical candies aligned horizontally or vertically
  2. 💥 Crush Simultaneously: Remove all matched candies at once (they become 0)
  3. 🍃 Apply Gravity: Let remaining candies fall down to fill empty spaces
  4. 🔄 Repeat: Continue until no more matches can be made

Return the final stable board configuration after all possible crushing is complete.

Note: No new candies appear from above - only existing candies fall due to gravity!

Input & Output

example_1.py — Basic Horizontal Match
$ 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],[512,2,3,3,3],[13,14,15,16,17]]
Output: [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,0],[210,5,0,5,0],[310,211,112,213,0],[410,311,3,313,114]]
💡 Note: The bottom row has three consecutive 3's which get crushed. After gravity, remaining candies fall down to fill empty spaces.
example_2.py — Vertical Match
$ Input: board = [[1,3,5,5,2],[3,4,3,3,1],[3,2,4,5,2],[2,4,4,5,5],[1,4,4,1,1]]
Output: [[1,3,0,0,0],[3,4,0,0,0],[3,2,0,0,0],[2,4,0,0,0],[1,4,3,5,5]]
💡 Note: Multiple vertical matches (5,5,5 and 3,3,3) are found and crushed simultaneously, then gravity is applied.
example_3.py — No Matches
$ Input: board = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,2,3],[4,5,6],[7,8,9]]
💡 Note: No groups of 3+ identical candies exist, so the board remains unchanged.

Constraints

  • m == board.length
  • n == board[i].length
  • 3 <= m, n <= 50
  • 1 <= board[i][j] <= 2000
  • board[i][j] == 0 represents an empty cell

Visualization

Tap to expand
Candy Crush - Board Simulation INPUT BOARD 0 1 2 3 4 5 6 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 512 2 3 3 3 13 14 15 16 17 7x5 grid with candies Red borders = matches found Match Pattern Found: Row 5: [3,3,3] horizontal Col 2: [3,3] vertical Col 3: [3,3] vertical Diagonal 5s detected ALGORITHM STEPS 1 Find Matches Scan for 3+ identical candies horizontally/vertically 2 Mark for Crushing Negate matched values (mark without removing) 3 Crush Simultaneously Set all marked cells to 0 (remove candies) 4 Apply Gravity Drop candies down to fill empty spaces REPEAT until stable No more matches found A 0 --> 0 A gravity FINAL RESULT 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 110 0 0 0 0 210 5 0 5 0 310 211 112 213 0 410 311 3
Asked in
King 45 Google 25 Amazon 18 Meta 12
43.5K Views
Medium Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen
Algorithm Visualization
Pinch to zoom • Tap outside to close
Test Cases
0 passed
0 failed
3 pending

Select Compiler

Choose a programming language

Compiler list would appear here...