awelgorithms

Young-tableau staircase versus binary search

Updated

A Young tableau increases along every row and every column. The staircase walk starts at top-right: if a[r][c] > target go left (c--), if a[r][c] < target go down (r++). That is O(R+C), not a log shrink.

What to focus on

Binary searching each row is O(R log C) and ignores the column order. Use the staircase when both directions increase; do not pretend the staircase is binary search.

Worked examples

staircase hit

a = [[1, 4, 9], [10, 11, 12]], target 11
  1. start (0,2)=9 < 11, r++
  2. (1,2)=12 > 11, c--
  3. (1,1)=11

Result: (1, 1) in 3 steps

staircase miss

a = [[1, 4, 9], [10, 11, 12]], target 8
  1. (0,2)=9 > 8, c--
  2. (0,1)=4 < 8, r++
  3. (1,1)=11 > 8, c--
  4. (1,0)=10 > 8, c-- past 0

Result: not found, O(R+C) walk

per-row binary

a = [[1, 4, 9], [10, 11, 12]], target 11
  1. binary search row 0 miss
  2. binary search row 1 hit col 1

Result: (1, 1) with two log C searches, not a staircase

Try it

Write staircase(a, t). a is a Young tableau. t is the target. Start at top-right cell (r, c). Return (r, c) or not found. Staircase t=11 → (1, 1) in 3 steps Staircase t=8 → not found Per-row binary t=11 → (1, 1), not a staircase

The matrix is a non-empty Young tableau: non-decreasing along every row and every column.