Skip to main content

Swim in Rising Water

Given n x n grid where grid[i][j] is elevation at (i,j) At time t, can swim if elevation <= t Find minimum time to reach bottom-right from top-left Example: grid = [[0,2],[1,3]] => 3

Swim in Rising Water is really a boundary-finding problem, even if it doesn't look like it at first. It's a hard one for a reason: it stresses boundary precision when the rules pile up. If you can state the invariant for your search space, you'll stop making off-by-one mistakes.

Examples

You write

swim_in_water(grid: List[List[int]])

  1. Example 1

    Input
    grid = [[0,2],[1,3]]
    returns
    3
  2. Example 2

    Input
    grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
    returns
    16

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Graphs

I reach for it when

  • The input is edges, prerequisites or connections.
  • You need cycles, an ordering or connected groups.

Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.

All 32 Graphs problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Find Minimum Time to Reach Destinationhard