Add Minimum Number of Rungs - Problem

Imagine you're standing at the bottom of a tall ladder, ready to climb to the top! ๐Ÿชœ The ladder has rungs at specific heights, but some gaps between rungs might be too large for you to safely climb.

You are given a strictly increasing integer array rungs that represents the height of existing rungs on the ladder. Starting from the floor at height 0, your goal is to reach the last (highest) rung.

However, there's a catch! You can only climb to the next rung if the distance between your current position and the next rung is at most dist. If the gap is too large, you'll need to add additional rungs at any positive integer heights to make the climb possible.

Your mission: Find the minimum number of rungs that must be added to make the entire ladder climbable!

Input & Output

example_1.py โ€” Basic Case
$ Input: rungs = [1,3,5,10], dist = 2
โ€บ Output: 2
๐Ÿ’ก Note: Starting at height 0: gap to rung 1 is 1 (โ‰ค2, OK), gap from 1 to 3 is 2 (โ‰ค2, OK), gap from 3 to 5 is 2 (โ‰ค2, OK), gap from 5 to 10 is 5 (>2, need 2 rungs at heights 7 and 9). Total: 2 rungs needed.
example_2.py โ€” Large Gap
$ Input: rungs = [3,6,8,10], dist = 3
โ€บ Output: 0
๐Ÿ’ก Note: All gaps are manageable: 0โ†’3 (gap=3, โ‰ค3), 3โ†’6 (gap=3, โ‰ค3), 6โ†’8 (gap=2, โ‰ค3), 8โ†’10 (gap=2, โ‰ค3). No additional rungs needed.
example_3.py โ€” Single Large Jump
$ Input: rungs = [10], dist = 2
โ€บ Output: 4
๐Ÿ’ก Note: From floor (height 0) to rung at height 10, gap = 10. Since dist = 2, we need โŒŠ(10-1)/2โŒ‹ = 4 intermediate rungs at heights 2, 4, 6, 8.

Visualization

Tap to expand
Floor (Start: Height 0)H=4H=15Gap=4, dist=3 โœ“Gap=11, dist=3 โœ—Need: โŒŠ(11-1)/3โŒ‹ = 3 rungs+H=7+H=10+H=13๐ŸŽฏ Goal!๐Ÿงฎ Key Formula: (gap - 1) รท max_stepThis ensures every sub-gap โ‰ค max_step distanceGreedy approach gives optimal solution!
Understanding the Visualization
1
Identify Large Gaps
Find gaps between rungs that exceed maximum step distance
2
Calculate Minimum Rungs
Use formula (gap-1)/dist to find minimum rungs needed
3
Place Optimally
Position new rungs to minimize total count while staying within constraints
Key Takeaway
๐ŸŽฏ Key Insight: Use the mathematical formula (gap-1)/dist to instantly calculate the minimum rungs needed for any gap, making this an elegant O(n) greedy solution!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * max_gap/dist)

For each of n rungs, we might add up to max_gap/dist intermediate rungs

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค rungs.length โ‰ค 105
  • 1 โ‰ค rungs[i] โ‰ค 109
  • 1 โ‰ค dist โ‰ค 109
  • rungs is strictly increasing
Asked in
Amazon 25 Google 18 Microsoft 12 Meta 8
28.4K Views
Medium Frequency
~12 min Avg. Time
890 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