Number of Black Blocks - Problem

You are given two integers m and n representing the dimensions of a 0-indexed m x n grid.

You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black. All cells in the grid that do not appear in coordinates are white.

A block is defined as a 2 x 2 submatrix of the grid. More formally, a block with cell [x, y] as its top-left corner where 0 <= x < m - 1 and 0 <= y < n - 1 contains the coordinates [x, y], [x + 1, y], [x, y + 1], and [x + 1, y + 1].

Return a 0-indexed integer array arr of size 5 such that arr[i] is the number of blocks that contains exactly i black cells.

Input & Output

Example 1 — Basic 3x3 Grid
$ Input: m = 3, n = 3, coordinates = [[0,0],[1,1]]
Output: [0,3,1,0,0]
💡 Note: There are 4 possible 2x2 blocks. Block (0,0) contains cells [(0,0),(0,1),(1,0),(1,1)] with 2 black cells at (0,0) and (1,1). Block (0,1) contains 1 black cell at (1,1). Block (1,0) contains 1 black cell at (1,1). Block (1,1) contains 1 black cell at (1,1). So we have 0 blocks with 0 black, 3 blocks with 1 black, 1 block with 2 black, and 0 blocks with 3+ black.
Example 2 — All Black 2x2
$ Input: m = 2, n = 2, coordinates = [[0,0],[0,1],[1,0],[1,1]]
Output: [0,0,0,0,1]
💡 Note: Only one 2x2 block possible at (0,0) containing all 4 black cells [(0,0),(0,1),(1,0),(1,1)], so result is [0,0,0,0,1].
Example 3 — No Black Cells
$ Input: m = 2, n = 2, coordinates = []
Output: [1,0,0,0,0]
💡 Note: Only one 2x2 block with no black cells, so result is [1,0,0,0,0].

Constraints

  • 1 ≤ m, n ≤ 105
  • 0 ≤ coordinates.length ≤ 104
  • coordinates[i].length == 2
  • 0 ≤ xi < m
  • 0 ≤ yi < n
  • It is guaranteed that coordinates contains pairwise distinct coordinates.

Visualization

Tap to expand
Number of Black Blocks INPUT 0,0 1,1 m = 3, n = 3 coordinates = [[0,0],[1,1]] Grid: 3x3 Black cells: 2 Possible 2x2 blocks: 4 ALGORITHM STEPS 1 Store Black Cells Use HashSet for O(1) lookup 2 For Each Black Cell Find all 2x2 blocks containing it 3 Count Black in Block Use HashMap: block-->count 4 Build Result Array arr[i] = blocks with i black 4 Possible 2x2 Blocks: 2 black 1 black 1 black 1 black Total: (m-1)*(n-1) = 4 blocks FINAL RESULT Block Count Summary: 0 black cells: 1 block 1 black cell: 3 blocks 2 black cells: 0 blocks 3 black cells: 0 blocks 4 black cells: 0 blocks Output Array: 1 [0] 3 [1] 0 [2] 0 [3] 0 [4] OK - Verified! [1, 3, 0, 0, 0] Key Insight: Each black cell can belong to at most 4 different 2x2 blocks. Using a HashMap to track black cell counts per block avoids checking all (m-1)*(n-1) blocks individually. Time Complexity: O(k) where k = number of black cells. Space: O(k) for the HashMap. TutorialsPoint - Number of Black Blocks | Hash Map Optimization
Asked in
Microsoft 25 Google 20
8.5K Views
Medium Frequency
~15 min Avg. Time
156 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