KatInterview

Distinct Value Sum Across Peak-Score Square Ring Windows

Arrays & HashingAuto-graded · 25 test cases
HardAuto-gradedArrays & Hashing2d-prefix-summatrixsliding-window

Last updated

Tests 2D prefix sums, border enumeration, multi-way tie tracking, and cross-frame value deduplication on square ring-shaped windows of a matrix.

Problem

You have a grid of integers with R rows and C columns, and a window side length w.

A window is any w × w block of cells aligned to the grid axes. Its top-left corner can be placed at any cell (r, c) satisfying r + w ≤ R and c + w ≤ C.

The ring of a window is the subset of its cells that lie on its outermost layer: the top row, the bottom row, the leftmost column, or the rightmost column of that block. Cells strictly inside the outermost layer are not part of the ring. Every cell on the ring is counted exactly once when computing the ring sum.

Your task:

  1. Compute the ring sum of every valid window and find the peak ring sum — the largest ring sum achieved by any window.
  2. Collect the values of all ring cells belonging to any window that achieves the peak ring sum.
  3. Return the sum of the distinct values in that collection.

Values from windows whose ring sum falls short of the peak do not contribute, even if those same cells happen to sit on the ring of a peak window.

Rules

  • When w = 1, the ring is the single cell (it is simultaneously its own top row, bottom row, left column, and right column).
  • When w = 2, every cell in the window is on its ring; there are no interior cells.
  • Distinctness is by value: if the number 5 appears on the rings of three different peak windows, it is added to the result only once. Likewise, if the same cell belongs to multiple peak windows' rings, its value is still counted once.
  • Negative integers are included as-is when they appear as ring values of peak windows.

Constraints

  • 1 ≤ R, C ≤ 100
  • Every row has exactly C elements.
  • 1 ≤ w ≤ min(R, C)
  • -10^4 ≤ grid[i][j] ≤ 10^4
  • The absolute value of the answer fits in a 32-bit signed integer (≤ 50,005,000).

Examples

Example 1

Input: peak_ring_distinct_sum([[1, 2, 3, 4], [5, 100, 7, 8], [9, 10, 11, 12]], 3)

Output: 150

Example 2

Input: peak_ring_distinct_sum([[1, 1, 1], [1, 5, 1], [1, 1, 1]], 2)

Output: 6

Example 3

Input: peak_ring_distinct_sum([[3, 7], [7, -2]], 1)

Output: 7

Hints

Hint 1

Compute the sum of any rectangular region in O(1) by building a 2D prefix-sum table first. Then express the ring sum as the full w×w square sum minus the (w−2)×(w−2) interior square sum — watch out for the cases where that interior is empty.

Hint 2

Track the peak ring sum and the top-left corners of all windows that achieve it in a single pass. Start your best-so-far variable at None rather than 0 so that all-negative grids are handled correctly.

Hint 3

Once you know the peak windows, mark every ring cell of each one in a boolean grid (top row, bottom row, left column, right column). Then sweep the grid: collect matrix values at marked cells into a set, and return the sum of that set.