The Furthest Building You Can Reach interview question asks you to navigate a series of buildings of varying heights. You start at building 0. To move to the next building, if it is taller, you must use either bricks or ladders to cover the height difference. You have a limited number of bricks and ladders. A ladder covers any height difference, while a brick covers exactly one unit of height. You want to find the index of the furthest building you can reach.
This is a very popular Greedy and Heap interview pattern problem asked by companies like Uber, Amazon, and Google. It tests your ability to optimize resource usage. The core challenge is deciding when to use a ladder versus bricks. You can't just greedily use bricks until they run out, because you might waste them on a massive jump that a ladder could have covered.
The problem is solved using a Greedy approach with a Min-Heap.
bricks count, and use the freed-up ladder for the current, larger jump.bricks drops below 0, you cannot make the current jump. Return the current building index.Heights: [4, 2, 7, 6, 9, 14, 12], Bricks: 5, Ladders: 1
4 -> 2: Downhill. Go to 1.2 -> 7: Diff 5. Use ladder. Heap: [5]. Go to 2.7 -> 6: Downhill. Go to 3.6 -> 9: Diff 3. No ladders left. Min jump in heap is 5.
bricks = 5 - 3 = 2. Heap remains [5]. Go to 4.9 -> 14: Diff 5. No ladders. Min jump in heap is 5.
This is the quintessential "retroactive greedy" problem. You make a choice (use a ladder), but keep a record of it so you can "undo" that choice later if a better opportunity arises. This is a very powerful pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Average Pass Ratio | Medium | Solve | |
| Maximal Score After Applying K Operations | Medium | Solve | |
| Make the Prefix Sum Non-negative | Medium | Solve | |
| Remove Stones to Minimize the Total | Medium | Solve | |
| Minimum Cost to Connect Sticks | Medium | Solve |