Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1936. Add Minimum Number of Rungs - Leetcode Solution

Code Implementation

class Solution:
    def addRungs(self, rungs, dist):
        prev = 0
        res = 0
        for rung in rungs:
            gap = rung - prev
            if gap > dist:
                # How many rungs needed in the gap?
                res += (gap - 1) // dist
            prev = rung
        return res
      
class Solution {
public:
    int addRungs(vector<int>& rungs, int dist) {
        int prev = 0, res = 0;
        for (int rung : rungs) {
            int gap = rung - prev;
            if (gap > dist) {
                res += (gap - 1) / dist;
            }
            prev = rung;
        }
        return res;
    }
};
      
class Solution {
    public int addRungs(int[] rungs, int dist) {
        int prev = 0, res = 0;
        for (int rung : rungs) {
            int gap = rung - prev;
            if (gap > dist) {
                res += (gap - 1) / dist;
            }
            prev = rung;
        }
        return res;
    }
}
      
var addRungs = function(rungs, dist) {
    let prev = 0, res = 0;
    for (let rung of rungs) {
        let gap = rung - prev;
        if (gap > dist) {
            res += Math.floor((gap - 1) / dist);
        }
        prev = rung;
    }
    return res;
};
      

Problem Description

You are given a list of integers rungs, representing the heights of rungs on a ladder in strictly increasing order, and an integer dist, representing the maximum distance you can climb in a single step.

Starting from the ground at height 0, you want to reach the top rung. However, if the gap between any two consecutive rungs (or between the ground and the first rung) is greater than dist, you must add additional rungs so that you can always climb up by at most dist units at a time.

The task is to determine the minimum number of additional rungs you need to add to the ladder so that you can reach the last rung, given the climbing constraint.

Constraints:

  • Each rung can be used at most once for calculation.
  • There is always one valid solution (you may add as many rungs as needed).
  • Do not reuse or overlap new rungs in your calculation.

Thought Process

The problem is about ensuring you can climb a ladder where each step up is at most dist units high. If any gap is too big, you must insert extra rungs to make the climb manageable.

A brute-force approach might be to simulate each climb, checking at every step if you can reach the next rung. If not, you could insert a rung at the maximum possible height within reach and repeat until you can reach the next rung. However, this would be inefficient for large ladders.

To optimize, we realize that for each gap larger than dist, we can compute directly how many extra rungs are needed by dividing the gap by dist. This avoids unnecessary iteration and makes the solution efficient.

Solution Approach

Let's break down the optimized solution step-by-step:

  1. Initialize variables:
    • Start from prev = 0 (the ground).
    • Set a counter res = 0 to track the number of added rungs.
  2. Iterate through each rung:
    • For each rung in rungs, calculate the gap: gap = rung - prev.
    • If gap > dist, you need to add rungs in between.
  3. Calculate the number of rungs needed:
    • The number of extra rungs needed in the gap is (gap - 1) // dist.
    • This formula ensures that after adding these rungs, all steps are at most dist units high.
  4. Update previous position:
    • Set prev = rung for the next iteration.
  5. Return the result:
    • After processing all rungs, return res as the answer.

This approach is efficient because it only requires a single pass through the list and simple arithmetic for each gap.

Example Walkthrough

Let's walk through an example:

Input: rungs = [1, 3, 6, 10, 15], dist = 2

Step-by-step:

  • Start at prev = 0.
  • First rung at 1: gap = 1 - 0 = 1 (no rungs needed).
  • Next rung at 3: gap = 3 - 1 = 2 (no rungs needed).
  • Next rung at 6: gap = 6 - 3 = 3
    • Since 3 > 2, need (3 - 1) // 2 = 1 extra rung.
  • Next rung at 10: gap = 10 - 6 = 4
    • Since 4 > 2, need (4 - 1) // 2 = 1 extra rung.
  • Next rung at 15: gap = 15 - 10 = 5
    • Since 5 > 2, need (5 - 1) // 2 = 2 extra rungs.
Total extra rungs needed: 1 + 1 + 2 = 4
Final answer: 4

Time and Space Complexity

Brute-force approach:

  • For each gap, you might simulate each climb, potentially leading to O(N * K) time, where K is the maximum gap size divided by dist.
  • Space complexity is O(1), as you only need a few variables.
Optimized approach:
  • Time complexity: O(N), where N is the number of rungs, since we process each rung exactly once.
  • Space complexity: O(1), as only a constant number of variables are used.

The optimized solution is much more efficient and scalable for large input sizes.

Summary

In this problem, we efficiently determine the minimum number of rungs needed to ensure you can always climb up by at most dist units at a time. By calculating the gap between each rung and the previous position, and using integer division to find how many extra rungs are needed, we avoid unnecessary simulation and achieve a simple, elegant O(N) solution.

The key insight is transforming the problem from step-by-step simulation to direct calculation, making the solution both fast and easy to implement.