1514. Shortest Always-Enough Window
A solar farm logs the energy produced each hour in the array nums; no hour produces a negative amount. The operator wants a rule of the form "any block of L consecutive hours must produce at least t units", and asks for the smallest block length L between 1 and n for which this rule holds for every block of that length in the log.
Return that smallest L. If even the block covering the whole log (L = n) produces fewer than t units, return -1. The threshold t may be negative or larger than 32-bit range, so it is a 64-bit value; a negative threshold is always met. Only one length is ever returned, so no tie-break is needed. Aim for O(n log n) time and O(n) extra space.
Example 1
- Input:
- nums = [3,0,4,1], t = 4
- Output:
- 3
- Explanation:
Length 1 fails (a 0 exists), length 2 fails (0+3 is 3), length 3 gives 7 and 5 which both pass.
Example 2
- Input:
- nums = [2,2,2], t = 7
- Output:
- -1
- Explanation:
The whole log only produces 6, which is below 7, so the answer is -1.
Example 3
- Input:
- nums = [1,9], t = -5
- Output:
- 1
- Explanation:
A negative threshold is always met, so length 1 is enough.
Constraints
1 ≤ n ≤ 2*105
0 ≤ nums[i] ≤ 104
-109 ≤ t ≤ 2*109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)