As reported: Amazon L5 backend online assessment (February 2025), question 2: an
n × mwhite canvas and an integerk;paint[i]is the cell painted at minutei; the canvas becomes beautiful once somek × ksquare is fully painted; return the minimum minute. A 2D prefix-sum solution timed out on the last two tests.
An n × m canvas starts white. At minute i (starting from 1) the cell paint[i−1] = [row, col] is painted (rows and columns are numbered from 1). Every cell is painted exactly once, so paint has n·m entries. The canvas is beautiful as soon as it contains a k × k square whose cells are all painted. Return the first minute at which it is beautiful.
Example
n = 2, m = 3, k = 2
paint = [[1,2],[2,3],[2,1],[1,3],[2,2],[1,1]]
Output: 5
After minute 5 the cells (1,2), (1,3), (2,2), (2,3) are all painted.
Constraints: 1 ≤ k ≤ min(n, m); 1 ≤ n·m ≤ 10⁵; paint is a permutation of all cells.
Code runner format: four lines: n, m, k, then paint as a JSON array of [row, col] pairs. Implement firstBeautifulMinute(n, m, k, paint); the harness prints the minute.