Furthest Building You Can Reach - Problem

Imagine you're a daredevil parkour athlete navigating a cityscape filled with buildings of varying heights! ๐Ÿข

You start at building 0 and want to reach as far as possible by jumping from one building to the next. Here's the challenge:

  • If the next building is lower or equal height, you can jump freely ๐Ÿฆ˜
  • If the next building is taller, you need help:
    • Use a ladder (works for any height difference) ๐Ÿชœ
    • OR use bricks equal to the height difference ๐Ÿงฑ

Given an array heights representing building heights, plus your limited supply of bricks and ladders, find the furthest building index you can reach using optimal strategy!

Goal: Maximize the distance traveled by using your resources wisely.

Input & Output

example_1.py โ€” Basic Example
$ Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
โ€บ Output: 4
๐Ÿ’ก Note: Starting at building 0 (height 4): Go to building 1 (height 2) - no resources needed. Go to building 2 (height 7) - use 5 bricks. Go to building 3 (height 6) - no resources needed. Go to building 4 (height 9) - use 1 ladder. Cannot reach building 5 (height 14) as we need 5 more units but have 0 bricks and 0 ladders.
example_2.py โ€” Ladder Heavy
$ Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
โ€บ Output: 7
๐Ÿ’ก Note: We can optimally use ladders for the two largest gaps (height differences of 8 and 15) and bricks for smaller gaps. This allows us to reach building 7 before getting stuck.
example_3.py โ€” All Decreasing
$ Input: heights = [14,3,19,3], bricks = 17, ladders = 0
โ€บ Output: 3
๐Ÿ’ก Note: From 14โ†’3 (no resources), 3โ†’19 (use 16 bricks), 19โ†’3 (no resources). We can reach the end with bricks to spare.

Constraints

  • 1 โ‰ค heights.length โ‰ค 105
  • 1 โ‰ค heights[i] โ‰ค 106
  • 0 โ‰ค bricks โ‰ค 109
  • 0 โ‰ค ladders โ‰ค heights.length
  • Note: Building indices are 0-based

Visualization

Tap to expand
Start+2+7+6๐Ÿชœ๐Ÿชœ๐Ÿงฑ๐Ÿง  Strategy Evolution1๏ธโƒฃ Use ladders initially: gaps[2,7,6]2๏ธโƒฃ Min-heap tracks ladder usage: [2,6,7]3๏ธโƒฃ Out of ladders? Use bricks for smallest (2)4๏ธโƒฃ Reassign ladder to current gapโœ… Always keep ladders for LARGEST gaps!๐ŸŽฏ Key InsightDon't assign ladders greedily as you go!Instead, always optimize to use laddersfor the LARGEST height differences seen so far.
Understanding the Visualization
1
Initial Strategy
Start by using ladders for every upward jump - they're the most flexible resource
2
Resource Tracking
Keep a min-heap of all height differences where you've used ladders
3
Smart Reallocation
When you run out of ladders, take back the ladder from the smallest gap and use bricks there instead
4
Optimal Assignment
Now use that freed ladder for the current (likely larger) gap - always keeping ladders for biggest obstacles
Key Takeaway
๐ŸŽฏ Key Insight: The optimal strategy is counter-intuitive - don't greedily assign ladders as you encounter gaps. Instead, use a priority queue to continuously optimize and ensure ladders are always used for the largest height differences encountered so far.
Asked in
Meta 35 Amazon 28 Google 22 Microsoft 18
82.1K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen