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 Hudson River Trading problems
- Cumulative Unique BytesOA · Seen Jul 2026
- Market Data Signal WatcherPHONE SCREEN · Seen Jul 2026
- Backtick Identifier ConverterOA · Seen Jun 2026
- Integer to String Without Built-insPHONE SCREEN · Seen May 2026
- Future Stock PricesOA · Seen Sep 2024
- Count Fancy NumbersOA · Seen Sep 2024
- Increasing Paths, part 2OA · Seen Aug 2024
- Increasing Paths, part 1OA · Seen Aug 2024