Sorting & Selection Easy
Binary Search on Sorted Threshold
GlobalWarmingImpl flattens and sorts an n × n altitude grid once, then binary-searches the first height strictly above each water level.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(n² log(n²)) preprocessing
AverageO(log(n²)) per water level
WorstO(log(n²)) per water level
SpaceO(n²) for the flattened altitude array
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Trace row-major preprocessing and the source's lower-bound-style binary search. Heights equal to the water level are flooded; the safe count is the sorted suffix after the first strictly higher altitude.
- A point is safe only when altitude > waterLevel; equality is flooded.
- The binary search returns the first index strictly greater than the level, after all equal duplicates.
- The constructor sorts once and reuses the same array for each query.
- The maximum check returns arr.length when every altitude is at or below the level.
- An empty matrix can be constructed, but querying it reaches the source's arr[-1] access and throws.
Algorithm code
JavaGlobalWarmingImpl.java (pinned repository source)Step linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1