Largest Local Values in a Matrix
Find largest value in every 3x3 submatrix Return (n-2) x (n-2) matrix where each cell is max of its 3x3 window Example: [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]] => [[9,9],[8,6]]
Given an n x n integer grid with n >= 3, return an (n - 2) x (n - 2) grid whose cell (i, j) holds the largest value in the 3 x 3 block with top-left corner grid[i][j]. Part 2 generalizes the block to k x k for 1 <= k <= n and returns an (n - k + 1) x (n - k + 1) grid. Part 3 moves to one dimension: given an array nums and a window size k, return the maximum of every contiguous window of length k, from left to right.
Examples
You write
largest_local(grid: List[List[int]])
Example 1
- Input
- grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
- returns
- [[9,9],[8,6]]
Example 2
- Input
- grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
- returns
- [[2,2,2],[2,2,2],[2,2,2]]
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.