-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJun-23-24.py
42 lines (39 loc) · 1.19 KB
/
Jun-23-24.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
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# TLE
# maxLen = 1
# i = 1
# while i<lem(nums)-1:
# j = i+1
# mi = nums[i]
# ma = nums[i]
# while j<len(nums):
# mi = min(mi,nums[j])
# ma = max(ma,nums[j])
# if abs(mi-ma)<=limit:
# maxLen = max(maxLen,j-i+1)
# else:
# i = j-1
# break
# j += 1
# i += 1
# return maxLen
minQ = deque()
maxQ = deque()
maxLen = 0
l = 0
for r in range(len(nums)):
while minQ and nums[r] < minQ[-1]:
minQ.pop()
while maxQ and nums[r] > maxQ[-1]:
maxQ.pop()
minQ.append(nums[r])
maxQ.append(nums[r])
while maxQ[0] - minQ[0] > limit:
if nums[l] == maxQ[0]:
maxQ.popleft()
if nums[l] == minQ[0]:
minQ.popleft()
l += 1
maxLen = max(maxLen,r - l + 1)
return maxLen