Loading...
Loading...
A delivery app records how many orders each rider completed per hour, for n hours in a row. Every value is positive. The manager asks: what is the shortest run of consecutive hours in which at least t orders were completed?
Print the length of the shortest such run. If even all n hours together have fewer than t orders, print 0.
0.Example 1
Input
6 7 2 3 1 2 4 3
Output
2
Hours 5–6 give 4 + 3 = 7 in two hours. No single hour reaches 7.
Example 2
Input
3 100 10 20 30
Output
0
All three hours give only 60.
Try the problem first. Open one hint at a time.
Values are positive, so making the window longer can only increase its total. That is what makes a sliding window work here.
Grow the window by moving right. While the total is at least t, record the length and shrink from the left.
Use a while (not an if) for shrinking: one new hour can let you drop several old ones.
Start the best length at "no answer yet" (such as n + 1) and print 0 if it never changed.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor