Largest Local Values in a Matrix II Problem
Description
LeetCode Problem 3933.
You are given an n x m integer matrix matrix containing non-negative integers.
A non-zero cell (row, col) checks the cells near it as follows:
- Let
x = matrix[row][col]. - Consider every cell within
xrows andxcolumns of(row, col). - Ignore cells that are outside the matrix.
- Ignore the cells where both the row distance and column distance are exactly
x.
The cell (row, col) is a local maximum if it is non-zero and no considered cell has a value greater than x.
Return an integer denoting the number of local maximums in matrix.
Example 1:
Input: matrix = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,2,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
Output: 1

Explanation:
- For the non-zero cell (3, 3), x = matrix[3][3] = 2.
- The highlighted cells are the considered cells within x rows and x columns of (3, 3).
- The four cells with both row and column distances equal to x = 2 are ignored.
- No considered cell has a value greater than 2, so (3, 3) is a local maximum.
- There are no other non-zero cells, so the answer is 1.
Example 2:
Input: matrix = [[1,2],[3,4]]
Output: 1
Explanation:
Only the cell with value 4 is a local maximum. Every other non-zero cell considers a cell with a greater value.
Example 3:
Input: matrix = [[1,0,1],[0,1,0],[1,0,1]]
Output: 5
Explanation:
- For a cell with value 1, the considered cells are the cell itself and its 4-directionally adjacent cells that are inside the matrix.
- Each of the five cells with value 1 only considers cells with values 0 or 1, so all five of them are local maximums.
Example 4:
Input: matrix = [[1,1],[1,1]]
Output: 4
Explanation:
All cells have the same value. Therefore, no cell considers another cell with a greater value, so all 4 cells are local maximums.
Constraints:
1 <= n == matrix.length <= 2001 <= m == matrix[i].length <= 2000 <= matrix[i][j] <= 200
Solution
Approach: Prefix Counts per Value
A non-zero cell with value x looks at the (2x + 1) x (2x + 1) square around it (clipped to the matrix) except the four corners. It is a local maximum when none of those cells is greater than x.
Counting "cells greater than x" in a square is a 2D prefix sum, but the threshold differs by cell. Values are at most 200, so group the non-zero cells by value, and for each value x that occurs, build the prefix sums of the indicator matrix > x once. Then for each cell with that value:
- count the cells greater than
xin the clipped square in O(1); - subtract the corners that lie inside the matrix and are greater than
x, since they are ignored; - if the result is 0, the cell is a local maximum.
Complexity: O(V * n * m) time for the prefix sums (V is the number of distinct values, at most 200) plus O(n * m) for the queries, and O(n * m) extra space.
from itertools import accumulate
class Solution:
def countLocalMaximums(self, matrix: List[List[int]]) -> int:
n, m = len(matrix), len(matrix[0])
cells = {}
for i in range(n):
for j in range(m):
if matrix[i][j] > 0:
cells.setdefault(matrix[i][j], []).append((i, j))
ans = 0
for x, group in cells.items():
# pre[i][j] = number of cells greater than x in the top-left i * j block
pre = [[0] * (m + 1)]
for row in matrix:
row_sums = accumulate(1 if a > x else 0 for a in row)
pre.append([0] + [up + s for up, s in zip(pre[-1][1:], row_sums)])
for i, j in group:
r0, r1 = max(0, i - x), min(n - 1, i + x)
c0, c1 = max(0, j - x), min(m - 1, j + x)
bigger = pre[r1 + 1][c1 + 1] - pre[r0][c1 + 1] - pre[r1 + 1][c0] + pre[r0][c0]
# the four corners at distance x in both directions are ignored
for ci in (i - x, i + x):
for cj in (j - x, j + x):
if 0 <= ci < n and 0 <= cj < m and matrix[ci][cj] > x:
bigger -= 1
if bigger == 0:
ans += 1
return ans