Net Climb Stretches
A cycling app splits each ride into kilometres and records each one's net height change: +40 for a kilometre that climbs 40 metres, -15 for one that drops 15. This month's challenge awards a badge for every stretch of back-to-back kilometres whose net climb is exactly the height of a famous tower.
Stretches can overlap, and any stretch of one or more kilometres counts. How many badges did your ride earn?
Your task
Write countStretches(changes, k). Return how many index pairs i ≤ j satisfy changes[i] + changes[i + 1] + ... + changes[j] === k.
Changes and k can be negative or zero. An empty ride returns 0.
countStretches(changes: int[], k: int) → int
Examples
changes = [2, 3, -1, 1, 4] k = 5
3
Three stretches add up to 5: kilometres 0 to 1 (2 + 3), 0 to 3 (2 + 3 - 1 + 1) and 3 to 4 (1 + 4). The first two overlap, and both count.
changes = [4, -4, 4] k = 0
2
A climb and a matching drop cancel. Kilometres 0 to 1 and 1 to 2 both net 0, so the answer is 2.
Constraints
- 0 ≤ changes.length ≤ 20,000
- -1,000 ≤ changes[i] ≤ 1,000
- -10,000,000 ≤ k ≤ 10,000,000
Start with brute force
For every start i, extend the end j one kilometre at a time with a running total, counting each time it equals k.
That is n(n + 1)/2 stretches, about 200 million for a 20,000 km log. A sliding window cannot help: with descents the total can drop as the window grows, so you never know whether to grow or shrink it.
Hints
0 of 4 shown- Hint 1
Write the running height after each kilometre of
[2, 3, -1, 1, 4], starting from 0 before the ride. How can you read the net climb of kilometres 3 to 4 off that list? - Hint 2
The running heights are 0, 2, 5, 4, 5, 9. The climb from kilometre
itojis (height afterj) minus (height beforei). For kilometres 3 to 4: 9 - 4 = 5. - Hint 3
So a stretch ending here is worth
kexactly when some earlier running height equalscurrent - k. The number of stretches ending here is the number of earlier heights with that value. - Hint 4
Walk once, keeping the running height and a map from height → times seen so far, starting with
{0: 1}for the start of the ride. At each kilometre, add the change, addcount[height - k]to the answer, then record the new height.
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
Track your running height from the start (a prefix sum). Any stretch's net climb is the height at its end minus the height just before its start. So at each kilometre ask: "how many earlier running heights were exactly k below me?" A map of height → count answers in one step.
Trace [2, 3, -1, 1, 4] with k = 5, starting with seen = {0: 1} (height 0 before the ride):
- The change +2 gives height 2. The map needs -3, which has not been seen, so no badge is added; then 2 is recorded. Badges: 0.
- The change +3 gives height 5. The map needs 0, which was seen once (the start), so kilometres 0 to 1 earn a badge. Badges: 1. Then 5 is recorded.
- The change -1 gives height 4. The map needs -1, which has not been seen, so 4 is only recorded.
- The change +1 gives height 5. The map needs 0, which was seen once, so kilometres 0 to 3 earn a badge. Badges: 2. The height 5 has now been reached twice.
- The change +4 gives height 9. The map needs 4, which was seen once (after kilometre 2), so kilometres 3 to 4 earn a badge. Badges: 3.
Three details matter. Seed the map with {0: 1}, or stretches starting at kilometre 0 are missed. Store counts, not just "seen", because each repeat of a height starts a different stretch. And look up before recording the current height, or with k = 0 you would count an empty stretch.
function countStretches(changes, k) {
const seen = new Map([[0, 1]]); // running height -> times reached
let height = 0, badges = 0;
for (const c of changes) {
height += c;
// every earlier height of (height - k) starts a stretch worth k
badges += seen.get(height - k) || 0;
seen.set(height, (seen.get(height) || 0) + 1);
}
return badges;
}def count_stretches(changes, k):
seen = {0: 1} # running height -> times reached
height = badges = 0
for c in changes:
height += c
# every earlier height of (height - k) starts a stretch worth k
badges += seen.get(height - k, 0)
seen[height] = seen.get(height, 0) + 1
return badgesRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.