1284. Tile Peak Map
A warehouse floor is described by the matrix grid, where each cell holds the pressure reading of one tile. A square sensor frame of side k is placed with its top-left corner on tile (r, c) and covers the k x k block of tiles from row r to row r + k - 1 and from column c to column c + k - 1. The frame must lie completely inside the grid.
Return a matrix with rows - k + 1 rows and cols - k + 1 columns whose cell (r, c) is the largest reading under the frame placed at (r, c). Readings can be negative. The work should not depend on k: aim for O(rows * cols) time by sliding a monotonic deque along every row first and then along every column of that intermediate result.
Example 1
- Input:
- grid = [[1,3,2],[4,2,1],[0,5,6]], k = 2
- Output:
- [[4,3],[5,6]]
- Explanation:
The four 2 x 2 blocks have maxima 4, 3, 5 and 6, which form the 2 x 2 result.
Example 2
- Input:
- grid = [[7]], k = 1
- Output:
- [[7]]
- Explanation:
A 1 x 1 grid with k = 1 returns the single value.
Example 3
- Input:
- grid = [[1,2,3],[6,5,4]], k = 2
- Output:
- [[6,5]]
- Explanation:
The two 2 x 2 blocks have maxima 6 and 5, giving a single row.
Constraints
- 1 ≤ rows, cols ≤ 30
- 1 ≤ k ≤ min(rows, cols)
- -109 ≤ grid[i][j] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(rows * cols)
- Space
- O(rows * cols)