We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d45c35c commit be03324Copy full SHA for be03324
1438.绝对差不超过限制的最长连续子数组/1438-绝对差不超过限制的最长连续子数组.py
@@ -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