Shortest to target
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
rain lists how many millimetres of rain fell on each day. All the numbers are positive.
Find the shortest stretch of days in a row whose rain adds up to at least target. Return how many days it has, or 0 if no stretch is enough.
Examples
Example 1
- Input
- rain = [1, 4, 2, 2, 5, 3], target = 8
- Output
- 2
5 + 3 = 8 takes 2 days. No single day has 8 mm.
Example 2
- Input
- rain = [2, 6, 6], target = 6
- Output
- 1
A single day of 6 mm is enough.
Example 3
- Input
- rain = [2, 2, 2], target = 7
- Output
- 0
All three days together only make 6 mm.
Rules
rainhas between 0 and 100,000 numbers, each from 1 to 10,000.targetis between 1 and 1,000,000,000.
Words on this page, in plain English (1)Open if any word is new to you
- Array (list)
- A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We want the fewest days in a row whose rain adds up to at least the target. Not exactly the target: going over is fine. If even all the days together aren't enough, the answer is 0.
The key fact: every day's rain is positive. So adding a day always makes a stretch's total bigger, and removing one always makes it smaller.
2. Solve an example by hand
[1, 4, 2, 2, 5, 3], target 8. Start adding: 1, 5, 7, 9. Days 0 to 3 reach 9, enough, 4 days long. Can it be shorter? Drop day 0: 8, still enough, now 3 days. Drop day 1: 4, not enough. So add day 4: 9, enough, 3 days. Drop day 2: 7, not enough. Add day 5: 10. Drop day 3: 8, still enough, now just 2 days. Drop day 4: 3, not enough, and there are no more days. The answer is 2.
3. The slow way
For each starting day, add days until the target is reached: O(n²) in the worst case. With 100,000 days of drizzle, that's billions of additions.
4. Spot the pattern
The shortest stretch of neighbouring items meeting a rule ("total at least the target"): a growing and shrinking window. Because the numbers are positive, the window behaves well:
- Too little? Only adding days can help: move
right. - Enough? Removing a day from the left might still be enough, and shorter: move
left.
5. The window idea
Grow until it's enough, then shrink
rain
- target =
- 8
- total =
- 1
- shortest =
- "none yet"
function shortestToTarget(rain, target) { let left = 0; let total = 0; let shortest = Infinity; for (let right = 0; right < rain.length; right++) { total += rain[right]; while (total >= target) { shortest = Math.min(shortest, right - left + 1); total -= rain[left]; left++; } } return shortest === Infinity ? 0 : shortest;}Add day 0 (1 mm). Total 1, not yet 8.
Each time the window reaches the target, we record its length, then shrink it as far as it stays enough. Every possible shortest stretch gets its turn as the window.
6. The code, line by line
let left = 0; let total = 0; let shortest = Infinity;shorteststarts atInfinityso any real length is smaller. We swap it for 0 at the end if nothing worked.for (let right = 0; right < rain.length; right++) { total += rain[right];Grow the window: add day
rightto the total.while (total >= target) { shortest = Math.min(shortest, right - left + 1); total -= rain[left]; left++; }While the window is enough, record its length, then try a shorter one by dropping the left day.
while, because dropping one day might still leave enough.return shortest === Infinity ? 0 : shortest;Infinitymeans no window was ever enough: return 0.
7. How fast is it?
| Every start | Sliding window | |
|---|---|---|
| Time | O(n²) | O(n): left and right each move at most n times |
| Extra memory | O(1) | O(1) |
Common mistakes
- Checking only when
total === target. "At least" means>=. - Using this when numbers can be negative. Then removing a day might raise the total, and the window logic breaks. That version needs a different idea (prefix sums with a sorted structure).
- Returning
Infinity. Remember to turn "never found" into 0.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Shortest to target
Read the 14 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function shortestToTarget(rain, target) { let left = 0; let total = 0; let shortest = Infinity; for (let right = 0; right < rain.length; right++) { total += rain[right]; while (total >= target) { shortest = Math.min(shortest, right - left + 1); total -= rain[left]; left++; } } return shortest === Infinity ? 0 : shortest;}