Sliding window · Medium
A batch planner receives positive record counts from consecutive partitions. It needs the shortest uninterrupted set of partitions that provides at least a target number of records. Partitions cannot be split or reordered.
Return the minimum length of a contiguous range whose sum is at least target, or 0 if none qualifies.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "nums": number[], "target": number }.
Fields
- nums lists strictly positive integer partition sizes.
- target is a strictly positive integer.
Output
Return value
- Return a length, not a sum or an index.
- A sum equal to target qualifies.
- If a single partition qualifies the answer is 1.
- Empty input or an insufficient total returns 0.
Constraints
- 0 <= nums.length <= 100000.
- 1 <= nums[i] <= 1000000000 and 1 <= target <= 100000000000000.
- Zero and negative partition sizes are outside this contract; they require a different approach.
Examples
Example 1
{
"nums": [
2,
3,
1,
2,
4,
3
],
"target": 7
}Returns
2The adjacent values 4 and 3 are the shortest qualifying range.
Example 2
{
"nums": [
8,
1
],
"target": 7
}Returns
1Exceeding the target is allowed.
Required complexity
O(n) time and O(1) extra space