-
Notifications
You must be signed in to change notification settings - Fork 0
/
subarray-atlease-ksum.py
48 lines (33 loc) · 1.03 KB
/
subarray-atlease-ksum.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
# 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
import collections;
class Solution(object):
def shortestSubarray(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
result=len(A)+1
p=[0]
for x in A:
p.append(p[-1]+x)
queue=collections.deque()
for idx,py in enumerate(p):
while queue and py<=p[queue[-1]]:
queue.pop()
while queue and py-p[queue[0]]>=K:
result=min(result,idx-queue.popleft())
queue.append(idx)
return result if result<len(A)+1 else -1
s=Solution()
print(s.shortestSubarray([84,-37,32,40,95],167))