forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest-subarray-with-sum-at-least-k.py
55 lines (49 loc) · 1.36 KB
/
shortest-subarray-with-sum-at-least-k.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Time: O(n)
# Space: O(n)
# Return the length of the shortest, non-empty,
# contiguous subarray of A with sum at least K.
# If there is no non-empty subarray with sum at least K, return -1.
#
# Example 1:
#
# Input: A = [1], K = 1
# Output: 1
# Example 2:
#
# Input: A = [1,2], K = 4
# Output: -1
# Example 3:
#
# Input: A = [2,-1,2], K = 3
# Output: 3
#
# Note:
# - 1 <= A.length <= 50000
# - -10 ^ 5 <= A[i] <= 10 ^ 5
# - 1 <= K <= 10 ^ 9
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
import collections
class Solution(object):
def shortestSubarray(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
accumulated_sum = [0]*(len(A)+1)
for i in xrange(len(A)):
accumulated_sum[i+1] = accumulated_sum[i]+A[i]
result = float("inf")
mono_increasing_q = collections.deque()
for i, curr in enumerate(accumulated_sum):
while mono_increasing_q and curr <= \
accumulated_sum[mono_increasing_q[-1]]:
mono_increasing_q.pop()
while mono_increasing_q and \
curr-accumulated_sum[mono_increasing_q[0]] >= K:
result = min(result, i-mono_increasing_q.popleft())
mono_increasing_q.append(i)
return result if result != float("inf") else -1