You are given a grid with m rows and n columns and a list coordinates of the black cells. Every cell not listed in coordinates is white. Count how many 2x2 sub-matrices contain exactly 0, 1, 2, 3 or 4 black cells.
Return an array of length 5 where element i is the number of 2x2 sub-matrices with exactly i black cells.
Function Signature
def count_black_submatrices(m: int, n: int, coordinates: list[list[int]]) -> list[int]:
Rules
-
Each coordinate is a
[row, column]
pair, zero-based. Row 0 is the top row and column 0 is the left column.
-
A
2x2
sub-matrix is two adjacent rows and two adjacent columns that lie fully inside the grid. It is identified by its top-left cell
(r, c)
with
0 <= r <= m - 2
and
0 <= c <= n - 2
, so there are
(m - 1) * (n - 1)
of them. Overlapping sub-matrices are counted separately, and the five counts add up to
(m - 1) * (n - 1)
.
Constraints
-
2 <= m, n <= 1000
-
0 <= len(coordinates) <= m * n
-
Each coordinate
[r, c]
satisfies
0 <= r < m
and
0 <= c < n
.
-
All coordinates are unique.
Examples
Input: m = 3, n = 3, coordinates = [[0, 0], [1, 1], [2, 2]]
Output: [0, 2, 2, 0, 0]
B W W
W B W
W W B
The black cells lie on the main diagonal (B is black, W is white). Of the four 2x2 sub-matrices, the top-left and bottom-right ones each hold 2 black cells, and the top-right and bottom-left ones each hold 1.
Input: m = 2, n = 2, coordinates = [[0, 0], [0, 1], [1, 0], [1, 1]]
Output: [0, 0, 0, 0, 1]
B B
B B
The grid is a single 2x2 sub-matrix, and all four of its cells are black.
Input: m = 2, n = 3, coordinates = []
Output: [2, 0, 0, 0, 0]
A 2 by 3 grid has two 2x2 sub-matrices, and with no black cells both hold 0.