Skip to content

Commit be03324

Browse files
committed
2020-06-16
1 parent d45c35c commit be03324

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def longestSubarray(self, nums, limit):
3+
"""
4+
:type nums: List[int]
5+
:type limit: int
6+
:rtype: int
7+
"""
8+
if not nums:
9+
return 0
10+
from heapq import *
11+
res = 1
12+
minHeap = []
13+
maxHeap = []
14+
pre = 0
15+
for i in range(0, len(nums)):
16+
heappush(minHeap, (nums[i], i))
17+
heappush(maxHeap, (-nums[i], i))
18+
while -minHeap[0][0] - maxHeap[0][0] > limit:
19+
while maxHeap and maxHeap[0][1] <= pre:
20+
heappop(maxHeap)
21+
while minHeap and minHeap[0][1] <= pre:
22+
heappop(minHeap)
23+
pre += 1
24+
res = max(res, i - pre + 1)
25+
return res

0 commit comments

Comments
 (0)