LeetCode 778 Swim in Rising Water - Solution

The pattern

Binary search on time + Union-Find for connectivity check.

Binary search the answer: "Can I reach the destination at time t?" At time t, all cells with elevation t\le t are accessible. Check if (0,0) and (n-1,n-1) are connected using only these cells.

For each candidate time t, union adjacent cells that both have elevation t\le t. Check if start and end share a root.

Alternative: Sort all cells by elevation. Add them one by one, unioning with already-added neighbors. Stop when start and end become connected.