Skip to content
BytePatterns

Longest Window Within Budget

MediumArrays#sliding-window#two-pointers~25m

Problem

Each day of a trip has a cost, and neither the costs nor the budget are ever negative. Given the list of daily costs and the budget, return the length of the longest run of consecutive days whose costs add up to at most the budget. If every single day is over budget on its own, return 0.

Examples

Input:  costs = [4, 1, 1, 3, 2, 6], budget = 6
Output: 3
Why:    days 1-3 cost 1 + 1 + 3 = 5, and no run of four days fits
Input:  costs = [0, 0, 2, 0], budget = 0
Output: 2
Why:    the two free days at the start are the longest run that costs nothing
Input:  costs = [9, 8], budget = 5
Output: 0
Why:    edge case, every single day is already over budget

Hints

0 / 3

Stuck on the idea rather than the code? Comparing Complexities covers it.