Prefix sum · Hard
A telemetry series records both positive and negative adjustments. An alert should identify the shortest uninterrupted period whose net total reaches a threshold. Negative readings prevent the usual positive-only sliding-window rule, and an empty period must never trigger an alert.
Return the smallest length of a non-empty contiguous segment whose signed sum is at least target or zero if none qualifies.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[], target: integer }
Fields
- nums contains signed integer adjustments in their original order; target is a signed threshold.
- A segment is a non-empty consecutive range of indexes. Negative values cannot be discarded or replaced by absolute values.
Output
Return value
- Return an integer length. Equality qualifies; a missing result is always zero, never null.
- Even if target is zero or negative, only non-empty segments count. Empty input returns zero.
Constraints
- 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000; -100000000000000 <= target <= 100000000000000.
- All prefix sums and results fit a safe JSON integer. Do not mutate input.
Examples
Example 1
{
"nums": [
2,
-1,
2
],
"target": 3
}Returns
3Example 2
{
"nums": [
1,
-2
],
"target": 4
}Returns
0Required complexity
O(n) time and O(n) auxiliary space