Sliding window · Medium
A mobile client replays a contiguous section of queued actions while keeping total transfer size within a budget.
Return the longest contiguous window whose total size is at most budget.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ sizes:integer[], budget:integer }
Input rules
- Sizes are non-negative and may be zero.
What each field means
sizes— non-negative replay sizes in their original order.budget— maximum combined size of the selected consecutive window.
Output
Return value and rules
- Return the largest number of consecutive entries whose sum does not exceed the budget.
- You may not skip entries inside the window. Zero-size entries still count toward its length.
- Empty input returns
0.
Constraints
- 0 <= sizes.length <= 100000; 0 <= size <= 1000000
- 0 <= budget <= 100000000000. A subsequence that skips actions is invalid.
Examples
Example 1
{
"budget": 4,
"sizes": [
2,
1,
3,
1
]
}Returns
2Example 2
{
"budget": 0,
"sizes": []
}Returns
0Required complexity
O(n) time and O(1) auxiliary space