Shortest Steep Climb
A cycling coach plans hill training on a mountain road split into equal segments. The map lists how many metres each segment climbs, in order. The road only goes up, so every number is positive.
Today's drill is to climb at least target metres without stopping, over as few segments as possible. The coach wants the shortest unbroken stretch that climbs that much.
Your task
Write shortestClimb(gains, target). Return the smallest number of consecutive segments whose gains add up to at least target.
If no stretch climbs enough, not even the whole road, return 0.
shortestClimb(gains: int[], target: int) → int
Examples
gains = [30, 10, 60, 20, 40, 50] target = 100
3
Segments 4 and 5 climb 40 + 50 = 90, which is just short. Segments 3 to 5 climb 20 + 40 + 50 = 110, and segments 0 to 2 climb exactly 100, which counts. No pair of neighbouring segments reaches 100, since the largest pair climbs 90, so the answer is 3.
gains = [5, 5, 5] target = 20
0
The whole road climbs only 15 metres, so no stretch reaches 20 and the answer is 0.
Constraints
- 0 ≤ gains.length ≤ 10,000
- 1 ≤ gains[i] ≤ 10,000
- 1 ≤ target ≤ 1,000,000,000
Start with brute force
For every start, add segments until the total reaches target, and keep the smallest count.
That is up to n steps for each of n starts, which is about 50 million additions for 10,000 segments and a large target. Each start re-adds almost the same segments as the previous one.
Hints
0 of 4 shown- Hint 1
In the first example, add segments from 0 until you reach 100. You stop at segment 2. If you now drop segment 0, do you still have enough?
- Hint 2
Once a stretch is long enough, a shorter one with the same right end can only come from removing segments on the left. Every gain is positive, so removing one always lowers the total.
- Hint 3
So use a window: grow it on the right until it climbs enough, then shrink it from the left while it still does, recording the length each time.
- Hint 4
Keep a
leftindex and a runningtotal. For eachright, addgains[right]to the total. Whiletotal ≥ target, recordright - left + 1if it is smaller than the best length so far, then subtractgains[left]and moveleftforward.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
With positive gains, adding a segment always raises the total and removing one always lowers it. So a sliding window is safe: grow the right edge until the stretch is steep enough, then pull in the left edge as far as it stays steep enough.
Trace [30, 10, 60, 20, 40, 50] with target 100:
- Add 30, 10 and 60. The total is 100 at
right= 2, which is enough, so the length is 3 and the best is 3. Dropping 30 leaves a total of 70, which is not enough, so shrinking stops. - Add 20. The total is 90, which is not enough.
- Add 40. The total is 130 and the window is segments 1 to 4, with length 4, which is not better. Dropping 10 leaves a total of 120 and length 3 (segments 2 to 4), which is not better either. Dropping 60 leaves a total of 60, so shrinking stops.
- Add 50. The total is 110 for segments 3 to 5, with length 3, which is not better. Dropping 20 leaves a total of 90, so shrinking stops.
The answer is 3, in about 2n steps: each segment enters the window once and leaves at most once. This approach needs positive gains, because with negative numbers dropping a segment could raise the total and the shrinking rule would break.
function shortestClimb(gains, target) {
let left = 0, total = 0, best = 0;
for (let right = 0; right < gains.length; right++) {
total += gains[right];
// Shrink from the left while the stretch still climbs enough.
while (total >= target) {
const len = right - left + 1;
if (best === 0 || len < best) best = len;
total -= gains[left];
left++;
}
}
return best;
}def shortest_climb(gains, target):
left = total = best = 0
for right, g in enumerate(gains):
total += g
# Shrink from the left while the stretch still climbs enough.
while total >= target:
length = right - left + 1
if best == 0 or length < best:
best = length
total -= gains[left]
left += 1
return bestRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.