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;
};
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:
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.
Let's break down the optimized solution step-by-step:
prev = 0
(the ground).res = 0
to track the number of added rungs.rungs
, calculate the gap: gap = rung - prev
.gap > dist
, you need to add rungs in between.(gap - 1) // dist
.dist
units high.prev = rung
for the next iteration.res
as the answer.This approach is efficient because it only requires a single pass through the list and simple arithmetic for each gap.
Let's walk through an example:
Input: rungs = [1, 3, 6, 10, 15]
, dist = 2
Step-by-step:
prev = 0
.gap = 1 - 0 = 1
(no rungs needed).gap = 3 - 1 = 2
(no rungs needed).gap = 6 - 3 = 3
3 > 2
, need (3 - 1) // 2 = 1
extra rung.gap = 10 - 6 = 4
4 > 2
, need (4 - 1) // 2 = 1
extra rung.gap = 15 - 10 = 5
5 > 2
, need (5 - 1) // 2 = 2
extra rungs.1 + 1 + 2 = 4
4
Brute-force approach:
dist
.The optimized solution is much more efficient and scalable for large input sizes.
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.