Problem · Array
Count 2x2 Submatrices by Black Cells
Learn this problemProblem statement
You are given a black-and-white grid with rows rows and cols columns. The array black contains the [row, column] coordinates of every black cell in the grid. All other cells are white, and the black-cell coordinates are pairwise unique.
For every blackCount from 0 through 4, count how many 2 x 2 submatrices contain exactly blackCount black cells.
Return an array result of length 5, where result[i] is the number of 2 x 2 submatrices containing exactly i black cells.
Function
solution(rows: int, cols: int, black: int[][]) → long[]Examples
Example 1
rows = 3cols = 3black = [[0, 0], [0, 1], [1, 0]]return = [1, 2, 0, 1, 0]There are four 2 x 2 submatrices:
- The submatrix with upper-left corner
(0, 0)contains3black cells. - The submatrix with upper-left corner
(0, 1)contains1black cell. - The submatrix with upper-left corner
(1, 0)contains1black cell. - The submatrix with upper-left corner
(1, 1)contains0black cells.
Therefore, the counts for 0 through 4 black cells are [1, 2, 0, 1, 0].
Constraints
2 <= rows <= 10^52 <= cols <= 10^50 <= black.length <= 500black[i].length = 20 <= black[i][0] < rows0 <= black[i][1] < cols- All coordinates in
blackare pairwise unique.
More Tiktok problems
- Concatenate Digit-wise SumsOA · Seen Aug 2026
- Count House Segments After DestructionOA · Seen Aug 2026
- Debugger Breakpoint ActionsOA · Seen Aug 2026
- Distribution Center Package AllocationOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Minimum Height Difference Between Distant PeaksOA · Seen Aug 2026
- Minimum Operations for Stepwise StructuresOA · Seen Aug 2026
- Rating Level After ChangesOA · Seen Aug 2026