Square Windows Containing Each Marked Cell
Arrays & HashingAuto-graded · 18 test casesLast updated
Map each marked grid cell to the sorted top-left corners of all in-bounds 2x2 windows that contain it, testing coordinate arithmetic and bounds handling.
Square Windows Containing Each Marked Cell
A grid has R rows and C columns, with rows indexed from 0 to R-1 and columns from 0 to C-1. You receive a list of marked cells, each given as [r, c].
A window is a 2x2 square of cells identified by its top-left corner (tr, tc). It occupies the cells (tr, tc), (tr, tc+1), (tr+1, tc) and (tr+1, tc+1). A window is valid only if it lies completely inside the grid, which means 0 <= tr <= R-2 and 0 <= tc <= C-2.
A window contains a marked cell if that cell is one of the four cells the window occupies.
Task
Implement a function that returns a dictionary describing, for every marked cell, which valid windows contain it.
- Each key is a string of the form
"r,c"(the cell's row and column joined by a comma, with no spaces), for example"1,1". - Each value is a list of
[tr, tc]pairs, one for every valid window that contains that cell. - Each value list must be sorted in ascending lexicographic order: by
trfirst, then bytc.
Rules and edge cases
- A marked cell always gets a key, even if no valid window contains it. In that case its value is an empty list
[]. - If the grid is too small to hold any 2x2 window (
R < 2orC < 2), every value is[]. - A marked cell lying outside the grid is not rejected. It still receives a key, and its list contains only those candidate windows that satisfy the validity condition (often this is empty).
- If the same cell appears more than once, it produces the same key, so the dictionary holds a single entry for it.
- If the list of marked cells is empty, return
{}.
Constraints
0 <= len(marked_cells); the list may be large relative to nothing else, so the running time should not depend onR * C.- Each marked cell is a list of two integers
[r, c]. RandCare integers and may be small (including less than 2).- The output must use string keys and lists of
[tr, tc]lists exactly as described.
Examples
Example 1
Input: get_windows_for_cells([[0, 0]], 3, 3)
Output: {'0,0': [[0, 0]]}
Example 2
Input: get_windows_for_cells([[1, 1]], 3, 3)
Output: {'1,1': [[0, 0], [0, 1], [1, 0], [1, 1]]}
Hints
Hint 1
A window with top-left corner (tr, tc) occupies rows tr and tr+1 and columns tc and tc+1. Turn this around: which values of tr can make row r part of a window?
Hint 2
Only tr = r-1 or tr = r can contain row r, and only tc = c-1 or tc = c can contain column c. That gives at most four candidate windows per marked cell, so there is no need to scan the grid.
Hint 3
Keep a candidate only if 0 <= tr <= R-2 and 0 <= tc <= C-2. Iterate tr and tc in increasing order (or sort at the end) so each list is lexicographically sorted, and build the key as the string f"{r},{c}".
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Instead of checking every window in the grid against the marked cells, reverse the question: for a given cell (r, c), which windows can contain it?
A window with top-left corner (tr, tc) covers rows tr and tr+1 and columns tc and tc+1. The cell (r, c) lies inside it exactly when tr is r-1 or r, and tc is c-1 or c. So there are at most four candidate corners per cell:
(r-1, c-1), (r-1, c), (r, c-1), (r, c).
For each candidate, keep it only if the whole window fits in the grid: 0 <= tr <= R-2 and 0 <= tc <= C-2. Store the surviving corners under the key "r,c".
Why it is correct
- Completeness: if a window covers
(r, c), thentr <= r <= tr+1, which forcestrto ber-1orr. The same holds fortc. So every containing window is among the four candidates. - Soundness: each candidate contains
(r, c)by construction, and the bounds check removes windows that stick out of the grid. - Ordering: looping
trin increasing order and thentcin increasing order already produces lexicographic order; the explicit sort on at most four items just makes this certain. - Edge cases: a grid with
R < 2orC < 2fails every bounds check, so all lists are empty. Cells outside the grid and duplicate cells are handled naturally, since a key is always written and duplicates simply overwrite the same entry with the same value.
Complexity
- Time: O(N) for N marked cells, since each cell examines four candidates. The grid size does not matter.
- Space: O(N) for the output, with at most four windows per entry.
Reference solution
from typing import Dict, List
def get_windows_for_cells(marked_cells: List[List[int]], R: int, C: int) -> Dict[str, List[List[int]]]:
result = {}
for r, c in marked_cells:
windows = []
# Only these four top-left corners can contain (r, c).
for tr in (r - 1, r):
for tc in (c - 1, c):
if 0 <= tr < R - 1 and 0 <= tc < C - 1:
windows.append([tr, tc])
windows.sort()
result[f"{r},{c}"] = windows
return result