Problem · Array

Bubble Explosion

Learn this problem
HardTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem statement

You are given a rectangular board bubbles. Every cell contains a positive integer representing a bubble color. Two cells are neighbors only when they share a side.

Perform exactly one simultaneous bubble explosion:

  • A bubble is eligible to explode when at least two of its neighboring cells contain bubbles of the same color.
  • Mark every eligible bubble and each of its same-colored neighboring bubbles.
  • Remove all marked bubbles simultaneously.
  • Within each column, let surviving bubbles fall downward while preserving their relative vertical order. Fill empty cells at the top with 0.

Return the board after this single explosion and gravity step.

Function

bubbleExplosion(bubbles: int[][]) → int[][]

Examples

Example 1

bubbles = [[3,1,2,1],[1,1,1,4],[3,1,2,2],[3,3,3,4]]return = [[0,0,0,1],[0,0,0,4],[0,0,2,2],[3,0,2,4]]

The eligible bubbles and their same-colored neighbors are removed at the same time. The remaining bubbles then fall to the bottom of their columns, and the newly empty cells are filled with 0.

Constraints

  • 1 <= bubbles.length <= 100
  • 1 <= bubbles[0].length <= 100
  • 1 <= bubbles[i][j] <= 10^4
  • Every row has the same length.

More Tiktok problems

drafts saved locally
public int[][] bubbleExplosion(int[][] bubbles) {
    // Write your code here.
}
bubbles[[3,1,2,1],[1,1,1,4],[3,1,2,2],[3,3,3,4]]
expected[[0,0,0,1],[0,0,0,4],[0,0,2,2],[3,0,2,4]]
checking account