workshop private

← all creations

Staircase

viz · created 2026-09-16

The LC 373 k-smallest-pair-sums walk drawn on the sum matrix it never builds — a min-heap of boundary cells, the taken region as a staircase that dents inward on every pop and heals as the neighbours are pushed, with the seeding switchable between one-parent-per-cell, a visited set, and the naive version that pops a cell twice.

algorithmsinterview-prepcanvas

Two sorted arrays on the axes, and the grid between them holds every pair sum. Rows are non-decreasing, columns are non-decreasing, and the grid as a whole is not sorted — which is the whole problem. Find K Pairs with Smallest Sums (LC 373) asks for the k smallest cells, and the only cells that can be the next one sit on the boundary of what has already been taken. So the piece draws that: a min-heap of boundary cells in amber, the taken region in teal with its edge drawn as a white staircase, and on every tick the root pops (the staircase dents inward by one cell) and the cells just past it are pushed (the dent heals, the boundary is whole again). The answer grows as a row of chips underneath.

Three ways to seed and expand the heap, switchable on the same instance:

Dim the grid (the d key) hides every cell the algorithm never touched. That is the part the brief was pointing at: the sums were only ever coordinates, the counter under the side pane says how many of the m × n cells were actually computed, and the matrix was a fiction the algorithm agreed to believe in for the length of the walk.

Presets: the LC example ([1,7,11] × [2,4,6], k = 3), a ties case ([1,1,2] × [1,2,3], where equal values are fine because dedup is on coordinates), and random sorted arrays up to 8 × 8. Step with the arrow keys, or let it run.

Reuse

src/staircase.js is a framework-free ES module:

No rendering or timers in the module; the canvas demo is reference code.

Gotchas