#1642

Furthest Building You Can Reach

Medium Min-heap of climbs given ladders

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Walk past buildings left to right. Climbing up costs bricks equal to the height difference, or one ladder. Return the furthest building index you can reach.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: climb as far as possible with limited bricks and ladders.

Why this works: ladders should go on the biggest climbs. Keep the climbs on ladders in a min-heap; when there are too many, pay bricks for the smallest.

Example: heights [4, 2, 7, 6, 9, 14, 12], bricks 5, ladders 1

climb 5 (2→7): ladderclimb 3 (6→9): heap {3, 5} → bricks pay 3 → 2 leftclimb 5 (9→14): pay 5? only 2 → stop at index 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Greedily using bricks first and ladders when bricks run out — that wastes ladders on small climbs.

Solve on LeetCode ↗
Complexity
  • Time: O(n log L), L = ladders.
  • Space: O(L).
Topic: Heaps & Priority Queue
🎬 Episodes 64–68: video coming soon