inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
ans = 0
maxq, minq = [], []
i = 0
for num in nums:
while maxq and maxq[-1] < num:
maxq.pop()
while minq and minq[-1] > num:
minq.pop()
maxq.append(num)
minq.append(num)
if maxq[0] - minq[0] > limit:
if maxq[0] == nums[i]:
maxq.pop(0)
if minq[0] == nums[i]:
minq.pop(0)
i += 1
return len(nums) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
hmin, hmax = [], []
heapq.heapify(hmin), heapq.heapify(hmax)
max_length = 0
i,j = 0, 0
heapq.heappush(hmin, (nums[0],0)), heapq.heappush(hmax, (-1*nums[0],0))
while j<len(nums):
while hmax[0][1]<i: heapq.heappop(hmax)
while hmin[0][1]<i: heapq.heappop(hmin)
if -1*(hmax[0][0]+hmin[0][0])<=limit:
max_length = max(max_length, j-i+1)
j+=1
if j>=len(nums): break
heapq.heappush(hmin, (nums[j], j))
heapq.heappush(hmax, (-1*nums[j], j))
else:
i+=1
return max_length
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxheap, minheap = [(-nums[0], 0)], [(nums[0], 0)]
l, maxindex, res = 0, 0, 1
for i, val in enumerate(nums[1:], 1):
if val - minheap[0][0] <= limit and - maxheap[0][0] -val <= limit:
res = max(res, i - maxindex + 1)
else:
while minheap and val - minheap[0][0] > limit:
v, index = heapq.heappop(minheap)
maxindex = max(maxindex, index + 1)
while maxheap and -maxheap[0][0] - val > limit:
v, index = heapq.heappop(maxheap)
maxindex = max(maxindex, index + 1)
heapq.heappush(minheap, (val, i))
heapq.heappush(maxheap, (-val, i))
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maximums = [(-nums[0], 0, )]
minimums = [(nums[0], 0, )]
left = 0 if len(nums) > 1 else 1
right = 1
while (right < len(nums)):
heapq.heappush(maximums, (-nums[right], right, ))
heapq.heappush(minimums, (nums[right], right, ))
right += 1
while (maximums[0][1] < left):
heapq.heappop(maximums)
while (minimums[0][1] < left):
heapq.heappop(minimums)
while (-maximums[0][0] - minimums[0][0] <= limit and right < len(nums)):
heapq.heappush(maximums, (-nums[right], right, ))
heapq.heappush(minimums, (nums[right], right, ))
right += 1
left += 1
if (-maximums[0][0] - minimums[0][0] <= limit):
return right - left + 1
else:
return right - left
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
l, r = 0, 0
res = 0
for r in range(len(nums)):
heapq.heappush(maxq, [-nums[r], r])
heapq.heappush(minq, [nums[r], r])
while -maxq[0][0] - minq[0][0] > limit:
# condition not satisfied, move left pointer ahead
l = min(maxq[0][1], minq[0][1])+1
#all values before left pointer are now useless
while maxq[0][1] < l:
heapq.heappop(maxq)
while minq[0][1] < l:
heapq.heappop(minq)
res = max(res, r-l+1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from heapq import heapify , heappop , heappush
class Solution:
def longestSubarray(self, lis: List[int], limit: int) -> int:
n = len(lis)
miheap=[]
mxheap=[]
heapify(miheap)
heapify(mxheap)
ans=1
heappush(miheap,[lis[0],0])
heappush(mxheap,[-lis[0],0])
tmp=0
for i in range(1,n):
heappush(miheap,[lis[i],i])
heappush(mxheap,[-lis[i],i])
while miheap and abs(miheap[0][0]+mxheap[0][0])>limit:
tmp = min(mxheap[0][1], miheap[0][1]) + 1
while mxheap[0][1] < tmp: heapq.heappop(mxheap)
while miheap[0][1] < tmp: heapq.heappop(miheap)
ans = max(ans,i-tmp+1)
#print(miheap,mxheap)
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j, a in enumerate(A):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
# can get a o(n logn runtime)
# use min and max heaps
from heapq import *
from collections import Counter
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_heap = []
max_heap = []
lazy_delete_min_heap = Counter()
lazy_delete_max_heap = Counter()
l = 0
longest = 0
for r in range(len(nums)):
heappush(min_heap, nums[r])
heappush(max_heap, -nums[r])
while abs(nums[r] - min_heap[0]) > limit or abs(nums[r] + max_heap[0]) > limit:
lazy_delete_min_heap[nums[l]] += 1
lazy_delete_max_heap[nums[l]] += 1
while lazy_delete_min_heap[min_heap[0]] > 0:
lazy_delete_min_heap[min_heap[0]] -= 1
heappop(min_heap)
while lazy_delete_max_heap[-max_heap[0]] > 0:
lazy_delete_max_heap[-max_heap[0]] -= 1
heappop(max_heap)
l += 1
longest = max(longest, r - l + 1)
return longest
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
res = 0
maxq = []
minq = []
j = 0
for i,n in enumerate(nums):
heapq.heappush(maxq, [-n,i] )
heapq.heappush(minq, [n,i] )
while -maxq[0][0] - minq[0][0] > limit:
ind = min( maxq[0][1], minq[0][1] )
while maxq[0][1] <= ind:
heapq.heappop(maxq)
while minq[0][1] <= ind:
heapq.heappop(minq)
j = ind+1
res = max(res, i - j +1 )
return res
# res = 0
# i = 0
# arr = [] # space O(res)
# for n in nums: # O(n) time
# bisect.insort(arr, n) # O(res)
# while arr[-1] - arr[0] > limit:
# ind = bisect.bisect_right(arr, nums[i]) # O( log res )
# arr.pop(ind-1) # O(res)
# i += 1
# res = max(res, len(arr))
# return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minq,maxq = [],[]
l = 0
res = 0
for i in range(len(nums)):
heapq.heappush(minq,[nums[i],i])
heapq.heappush(maxq,[-nums[i],i])
while -maxq[0][0] - minq[0][0] > limit:
l = min(maxq[0][1], minq[0][1])+1
while maxq[0][1] < l:
heapq.heappop(maxq)
while minq[0][1] < l:
heapq.heappop(minq)
res = max(res,i-l+1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums, limit):
maxQ, minQ = deque(), deque()
i = 0
res = 0
for j, val in enumerate(nums):
while maxQ and val > maxQ[-1]: maxQ.pop()
while minQ and val < minQ[-1]: minQ.pop()
maxQ.append(val)
minQ.append(val)
if maxQ[0] - minQ[0] > limit:
if maxQ[0] == nums[i]: maxQ.popleft()
if minQ[0] == nums[i]: minQ.popleft()
i += 1
#res = max(res, j-i+1)
return len(nums) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque, max_deque = deque(), deque()
l = r = 0
ans = 0
while r < len(nums):
while min_deque and nums[r] <= nums[min_deque[-1]]:
min_deque.pop()
while max_deque and nums[r] >= nums[max_deque[-1]]:
max_deque.pop()
min_deque.append(r)
max_deque.append(r)
if nums[max_deque[0]] - nums[min_deque[0]] <= limit:
ans = max(ans, r - l + 1)
r += 1
elif nums[max_deque[0]] - nums[min_deque[0]] > limit:
while min_deque[0] <= l:
min_deque.popleft()
while max_deque[0] <= l:
max_deque.popleft()
l += 1
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq=[]
minq=[]
res=0
i=0
for j,c in enumerate(nums):
heapq.heappush(maxq,[-c,j])
heapq.heappush(minq,[c,j])
while -maxq[0][0]-minq[0][0]>limit:
i=min(maxq[0][1],minq[0][1])+1
while maxq[0][1]<i:
heapq.heappop(maxq)
while minq[0][1]<i:
heapq.heappop(minq)
res=max(res,j-i+1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# minheap=[]
# maxheap=[]
# heapq.heapify(minheap)
# heapq.heapify(maxheap)
# length=1
# i=0
# j=1
# heapq.heappush(minheap,[nums[0],0]) # add first element and its index
# heapq.heappush(maxheap,[-nums[0],0])
# while j < len(nums):
# if nums[j]-minheap[0][0]<=limit and maxheap[0][0]*-1-nums[j]<= limit:
# length=max(length,j-i+1)
# heapq.heappush(minheap,[nums[j],j])
# heapq.heappush(maxheap,[-nums[j],j])
# j=j+1
# else:
# while len(minheap)>0 and nums[j]-minheap[0][0]>limit:
# ele,index=heapq.heappop(minheap)
# i=max(i,index + 1)
# while len(maxheap)>0 and -maxheap[0][0]-nums[j]>limit:
# ele,index=heapq.heappop(maxheap)
# i=max(i,index + 1)
# # i=maxindex+1 # update i and now we are not concerned with element before ith index
# heapq.heappush(minheap,[nums[j],j]) # add element and its index
# heapq.heappush(maxheap,[-nums[j],j])
# j=j+1
# return length
begin = end = 0
l = len(nums)
d = 0
heap_min = []
heap_max = []
heapq.heapify(heap_min)
heapq.heapify(heap_max)
while end < l:
heapq.heappush(heap_min, (nums[end], end))
heapq.heappush(heap_max, (-1 * nums[end], end))
while len(heap_min) > 0 and nums[end] - heap_min[0][0] > limit:
value, idx = heapq.heappop(heap_min)
begin = max(begin, idx + 1)
while len(heap_max) > 0 and -heap_max[0][0] - nums[end] > limit:
value, idx = heapq.heappop(heap_max)
begin = max(begin, idx + 1)
if end - begin + 1 > d:
d = end - begin + 1
end += 1
return d
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
N = len(nums)
if N == 0:
return 0
minQ = []
maxQ = []
l = 0
maxL = 0
for r in range(N):
num = nums[r]
idx_rightmost = l - 1
while minQ and (num - minQ[0][0] > limit or minQ[0][1]<=idx_rightmost):
[val,idx] = heapq.heappop(minQ)
idx_rightmost = max(idx_rightmost,idx)
while maxQ and (-maxQ[0][0] - num > limit or maxQ[0][1]<=idx_rightmost):
[val,idx] = heapq.heappop(maxQ)
idx_rightmost = max(idx_rightmost,idx)
l = idx_rightmost + 1
heapq.heappush(minQ, [nums[r],r])
heapq.heappush(maxQ, [-nums[r],r])
maxL = max(maxL, r-l+1)
return maxL
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
small = []
large = []
res = 1
left = 0
for i in range(len(nums)):
while small and nums[small[-1]] > nums[i]:
small.pop()
while large and nums[large[-1]] < nums[i]:
large.pop()
small.append(i)
large.append(i)
if small and large and nums[large[0]] - nums[small[0]] > limit:
left += 1
if small[0] < left:
small.pop(0)
if large[0] < left:
large.pop(0)
res = max(res, i-left+1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i:
heapq.heappop(maxq)
while minq[0][1] < i:
heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# sliding window, but we only need to keep the min and max in this window along with the index. at the same time, we set a variable, to check what is the window starting point, we will do
length = len(nums)
start = 0
min_heap = []
max_heap = []
res = 1
for i, num in enumerate(nums):
heapq.heappush(min_heap, (num, -i))
heapq.heappush(max_heap, (-num, -i))
# while -min_heap[0][1] < start:
# heapq.heappop(min_heap)
# while -max_heap[0][1] < start:
# heapq.heappop(max_heap)
### bug bug here: 加入出现现在的number导致超过了limit,那么你需要一直移动自动你找到一个index;
while -max_heap[0][0] - min_heap[0][0] > limit:
start += 1
while -min_heap[0][1] < start:
heapq.heappop(min_heap)
while -max_heap[0][1] < start:
heapq.heappop(max_heap)
res = max(res, i - start + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minQue = deque()
maxQue = deque()
start, end = 0, 0
longest = 1
while end < len(nums):
current = nums[end]
while minQue and nums[minQue[-1]] >= current:
minQue.pop()
minQue.append(end)
while maxQue and nums[maxQue[-1]] <= current:
maxQue.pop()
maxQue.append(end)
if nums[maxQue[0]] - nums[minQue[0]] > limit:
start += 1
while maxQue and start > maxQue[0]:
maxQue.popleft()
while minQue and start > minQue[0]:
minQue.popleft()
else:
longest = max(longest, end - start + 1)
end += 1
return longest
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
from collections import deque
mnq = deque([])
mxq = deque([])
left = 0
for right, num in enumerate(nums):
while mxq and mxq[-1] < num:
mxq.pop()
while mnq and mnq[-1] > num:
mnq.pop()
mxq.append(num)
mnq.append(num)
if mxq[0] - mnq[0] > limit:
if mxq[0] == nums[left]:
mxq.popleft()
if mnq[0] == nums[left]:
mnq.popleft()
left += 1
return len(nums) - left
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# Solution 1: Use two heaps
# Time O(NogN)
# Space O(N)
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
hq = deque()
hq.append([nums[0], 0])
lq = deque()
lq.append([nums[0], 0])
l = 0
ans = 1
for i in range(1, len(nums)):
n = nums[i]
while hq[0][1] < l:
hq.popleft()
while lq[0][1] < l:
lq.popleft()
while hq and hq[-1][0] <= n:
hq.pop()
else:
hq.append([n, i])
while lq and lq[-1][0] >= n:
lq.pop()
else:
lq.append([n, i])
if hq[0][0] - n > limit:
ans = max(ans, i - l)
while hq[0][0] - n > limit:
l = hq[0][1] + 1
hq.popleft()
elif n - lq[0][0] > limit:
ans = max(ans, i - l)
while n - lq[0][0] > limit:
l = lq[0][1] + 1
lq.popleft()
else:
ans = max(ans, i - l + 1)
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A: List[int], limit: int) -> int:
import heapq
maxd = []
mind = []
res = i = 0
for j, a in enumerate(A):
heapq.heappush(maxd, [-a, j])
heapq.heappush(mind, [a, j])
while - maxd[0][0] - mind[0][0] > limit:
i = min(maxd[0][1], mind[0][1]) + 1
while maxd[0][1] < i: heapq.heappop(maxd)
while mind[0][1] < i: heapq.heappop(mind)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
begin, end = 0, 0
n = len(nums)
heap_min = []
heapq.heapify(heap_min)
heap_max = []
heapq.heapify(heap_max)
heapq.heappush(heap_min, (nums[0], 0))
heapq.heappush(heap_max, (-nums[0], 0))
max_length = 1
while end < n - 1:
end += 1
if abs(nums[end] - heap_min[0][0]) > limit or abs(-heap_max[0][0] - nums[end]) > limit: # Need to know the min and max
# Make it valid!
while len(heap_min) > 0 and nums[end] - heap_min[0][0] > limit:
value, idx = heapq.heappop(heap_min)
begin = max(begin, idx + 1)
while len(heap_max) > 0 and -heap_max[0][0] - nums[end] > limit:
value, idx = heapq.heappop(heap_max)
begin = max(begin, idx + 1)
heapq.heappush(heap_min, (nums[end], end))
heapq.heappush(heap_max, (-nums[end], end))
max_length = max(max_length, end - begin + 1)
return max_length
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
'''
1. example: [8,2,4,7]
- Subarrays can be created by taking one element at a time and moving forward
- difference can be found by finding the difference between two elements in the array.
'''
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
res = i = 0
maxh = []
minh = []
for j, val in enumerate(nums):
heapq.heappush(maxh, [-val, j])
heapq.heappush(minh, [val, j])
while -maxh[0][0] - minh[0][0] > limit:
i = min(maxh[0][1], minh[0][1]) + 1
while maxh[0][1] < i:
heapq.heappop(maxh)
while minh[0][1] < i:
heapq.heappop(minh)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class minMonotonicQueue(object):
def __init__(self):
self.queue = collections.deque([]) # non-descreasing
def push(self, num, idx):
while len(self.queue) != 0 and self.queue[-1][0] > num:
self.queue.pop()
self.queue.append((num, idx))
def pop(self, idx):
if self.queue[0][1] == idx:
self.queue.popleft()
class maxMonotonicQueue(object):
def __init__(self):
self.queue = collections.deque([]) # non-increasing
def push(self, num, idx):
while len(self.queue) != 0 and self.queue[-1][0] < num:
self.queue.pop()
self.queue.append((num, idx))
def pop(self, idx):
if self.queue[0][1] == idx:
self.queue.popleft()
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
ptr1, ptr2 = 0, 0
res = 0
minQ, maxQ = minMonotonicQueue(), maxMonotonicQueue()
while ptr1 < len(nums) and ptr2 < len(nums):
# push
minQ.push(nums[ptr2], ptr2)
maxQ.push(nums[ptr2], ptr2)
# print (ptr1, ptr2, minQ.queue, maxQ.queue)
while maxQ.queue[0][0] - minQ.queue[0][0] > limit:
ptr1 += 1
if ptr1 > maxQ.queue[0][1]:
maxQ.pop(maxQ.queue[0][1])
if ptr1 > minQ.queue[0][1]:
minQ.pop(minQ.queue[0][1])
# print ('*', ptr1, ptr2, minQ.queue, maxQ.queue)
res = max(res, ptr2 - ptr1 + 1)
ptr2 += 1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
res = 1
slow = 0
fast = 0
min_queue = deque()
max_queue = deque()
while fast < len(nums):
while min_queue and nums[fast] <= nums[min_queue[-1]]:
min_queue.pop()
while max_queue and nums[fast] >= nums[max_queue[-1]]:
max_queue.pop()
min_queue.append(fast)
max_queue.append(fast)
# print(min_queue, max_queue)
while nums[max_queue[0]] - nums[min_queue[0]] > limit:
slow += 1
if max_queue[0] < slow:
max_queue.popleft()
if min_queue[0] < slow:
min_queue.popleft()
res = max(res, fast - slow + 1)
fast += 1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A, limit):
maxq, minq = [], []
res = i = 0
for j, a in enumerate(A):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums, limit):
maxQ, minQ = deque(), deque()
i = 0
for j, val in enumerate(nums):
while maxQ and val > maxQ[-1]: maxQ.pop()
while minQ and val < minQ[-1]: minQ.pop()
maxQ.append(val)
minQ.append(val)
if maxQ[0] - minQ[0] > limit:
if maxQ[0] == nums[i]: maxQ.popleft()
elif minQ[0] == nums[i]: minQ.popleft()
i += 1
return len(nums) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_q = deque()
min_q = deque()
left = 0
result = 0
for i, n in enumerate(nums):
while max_q and nums[max_q[-1]] <= n:
max_q.pop()
max_q.append(i)
while min_q and nums[min_q[-1]] >= n:
min_q.pop()
min_q.append(i)
while left < i and nums[max_q[0]] - nums[min_q[0]] > limit:
if left == max_q[0]:
max_q.popleft()
if left == min_q[0]:
min_q.popleft()
left += 1
result = max(result, i - left + 1)
return result
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq = []
minq = []
left = 0
res = 0
for right,num in enumerate(nums):
heapq.heappush(maxq, [-num, right])
heapq.heappush(minq, [num, right])
while - maxq[0][0] - minq[0][0] > limit:
left = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < left:
heapq.heappop(maxq)
while minq[0][1] < left:
heapq.heappop(minq)
res = max(res, right - left + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# dp = [[0 for _ in range(len(nums))] for _ in range(len(nums))]
# result = 1
# for i in range(len(nums)-2, -1, -1):
# for j in range(i+1, len(nums)):
# dp[i][j] = max(dp[i][j-1], dp[i+1][j] ,abs(nums[i] - nums[j]))
# # print(dp[i][j])
# if dp[i][j] <= limit:
# result = max(result, j - i + 1)
# return result
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
# print(maxq, minq)
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
# print(maxq)
# print(minq)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for pos, val in enumerate(nums):
heapq.heappush(maxq, [-val, pos])
heapq.heappush(minq, [val, pos])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, pos - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# sliding window, but we only need to keep the min and max in this window along with the index. at the same time, we set a variable, to check what is the window starting point, we will do
length = len(nums)
start = 0
min_heap = []
max_heap = []
res = 1
for i, num in enumerate(nums):
heapq.heappush(min_heap, (num, -i))
heapq.heappush(max_heap, (-num, -i))
while -min_heap[0][1] < start:
heapq.heappop(min_heap)
while -max_heap[0][1] < start:
heapq.heappop(max_heap)
# if num == 96:
# print(num)
### bug bug here: 加入出现现在的number导致超过了limit,那么你需要一直移动自动你找到一个index;
while -max_heap[0][0] - min_heap[0][0] > limit:
start += 1
while -min_heap[0][1] < start:
heapq.heappop(min_heap)
while -max_heap[0][1] < start:
heapq.heappop(max_heap)
# if num == min_heap[0][0]:
# start = -max_heap[0][1]+1
# if num == -max_heap[0][0]:
# start = -min_heap[0][1]+1
res = max(res, i - start + 1)
#print(i, start, res)
#if num == 3:
#print(sorted(max_heap))
#print(sorted(min_heap))
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
best = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, (-a, j))
heapq.heappush(minq, (a, j))
while (-maxq[0][0] - minq[0][0]) > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
best = max(best, j -i + 1)
return best
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j, a in enumerate(nums):
heapq.heappush(maxq, [-a, j])
heapq.heappush(minq, [a, j])
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while maxq[0][1] < i: heapq.heappop(maxq)
while minq[0][1] < i: heapq.heappop(minq)
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums, limit):
maxQ, minQ = deque(), deque()
i = 0
for j, val in enumerate(nums):
while maxQ and val > maxQ[-1]: maxQ.pop()
while minQ and val < minQ[-1]: minQ.pop()
maxQ.append(val)
minQ.append(val)
if maxQ[0] - minQ[0] > limit:
if maxQ[0] == nums[i]: maxQ.popleft()
if minQ[0] == nums[i]: minQ.popleft()
i += 1
return len(nums) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxd, mind = collections.deque(), collections.deque()
l, r, res = 0, 0, 0
while r < len(nums):
while len(maxd) and maxd[-1] < nums[r]:
maxd.pop()
while len(mind) and mind[-1] > nums[r]:
mind.pop()
maxd.append(nums[r])
mind.append(nums[r])
while maxd[0] - mind[0] > limit:
if nums[l] == maxd[0]:
maxd.popleft()
if nums[l] == mind[0]:
mind.popleft()
l += 1
res = max(r-l+1, res)
r += 1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
if len(nums) == 1:
return 1
left = 0
right = 0
curr_min = nums[0]
curr_max = nums[0]
max_len = 1
# while left <= right and right < len(nums):
for right in range(len(nums)):
curr_max = max(curr_max, nums[right])
curr_min = min(curr_min, nums[right])
if curr_max - curr_min <= limit:
max_len = max(max_len, right - left + 1)
else:
if nums[left] == curr_max:
curr_max = max(nums[left + 1: right + 1])
if nums[left] == curr_min:
curr_min = min(nums[left + 1: right + 1])
left += 1
return max_len
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
currmax = currmin = nums[0]
start = 0
res = 1
for i in range(1,len(nums)):
currmax = max(currmax,nums[i])
currmin = min(currmin,nums[i])
if currmax - currmin <= limit:
res = max(res,i-start+1)
else:
if currmax == nums[start]:
currmax = max(nums[start+1:i+1])
if currmin == nums[start]:
currmin = min(nums[start+1: i+1])
start += 1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
if len(nums) == 1:
return 1
left = 0
right = 0
curr_min = nums[0]
curr_max = nums[0]
max_len = 1
# while left <= right and right < len(nums):
for right in range(len(nums)):
curr_max = max(curr_max, nums[right])
curr_min = min(curr_min, nums[right])
if curr_max - curr_min <= limit:
max_len = max(max_len, right - left + 1)
else:
if nums[left] == curr_max:
curr_max = max(nums[left + 1: right + 1])
if nums[left] == curr_min:
curr_min = min(nums[left + 1: right + 1])
left += 1
right += 1
return max_len
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
left=0
n=len(nums)
l=0
r=1
maxVal=nums[0]
minVal=nums[0]
res=1
while(l<r and r<n):
maxVal=max(maxVal,nums[r])
minVal=min(minVal,nums[r])
if(maxVal-minVal<=limit):
res=max(res,r-l+1)
else:
if(nums[l]==maxVal):
maxVal=max(nums[l+1:r+1])
if(nums[l]==minVal):
minVal=min(nums[l+1:r+1])
l+=1
r+=1
return res
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_queue = collections.deque()
min_queue = collections.deque()
l = r = 0
max_len = 0
while r < len(nums):
while max_queue and nums[r] >= max_queue[-1][0]:
max_queue.pop()
max_queue.append((nums[r], r))
while min_queue and nums[r] <= min_queue[-1][0]:
min_queue.pop()
min_queue.append((nums[r], r))
r += 1
while max_queue[0][0] - min_queue[0][0] > limit:
l += 1
if max_queue[0][1] < l:
max_queue.popleft()
if min_queue[0][1] < l:
min_queue.popleft()
max_len = max(max_len, r - l)
return max_len
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from heapq import heappush, heapify, heappop
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_subsize, right, left = 0, 0, 0
max_vals, min_vals = [], []
heapify(max_vals)
heapify(min_vals)
while right < len(nums):
diff = max_vals[0][0] * -1 - min_vals[0][0] if max_vals and min_vals else 0
#print(diff, left, right)
if diff <= limit:
max_subsize = max(max_subsize, right - left)
heappush(min_vals, (nums[right], right))
heappush(max_vals, (nums[right] * -1, right))
right += 1
else:
left += 1
while max_vals and max_vals[0][1] < left:
heappop(max_vals)
while min_vals and min_vals[0][1] < left:
heappop(min_vals)
diff = max_vals[0][0] * -1 - min_vals[0][0] if max_vals and min_vals else 0
if diff <= limit:
max_subsize = max(max_subsize, right - left)
return max_subsize
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minheap=[]
maxheap=[]
heapq.heapify(minheap)
heapq.heapify(maxheap)
length=1
i=0
j=1
heapq.heappush(minheap,[nums[0],0]) # add first element and its index
heapq.heappush(maxheap,[-nums[0],0])
while j < len(nums):
if abs(minheap[0][0]-nums[j])<=limit and abs(abs(maxheap[0][0])-nums[j])<= limit:
length=max(length,j-i+1)
heapq.heappush(minheap,[nums[j],j])
heapq.heappush(maxheap,[-nums[j],j])
j=j+1
else:
while len(minheap)>0 and abs(minheap[0][0]-nums[j])>limit:
ele,index=heapq.heappop(minheap)
i=max(i,index + 1)
while len(maxheap)>0 and abs(-maxheap[0][0]-nums[j])>limit:
ele,index=heapq.heappop(maxheap)
i=max(i,index + 1)
# i=maxindex+1 # update i and now we are not concerned with element before ith index
heapq.heappush(minheap,[nums[j],j]) # add element and its index
heapq.heappush(maxheap,[-nums[j],j])
j=j+1
return length
# begin = end = 0
# l = len(nums)
# d = 0
# heap_min = []
# heap_max = []
# heapq.heapify(heap_min)
# heapq.heapify(heap_max)
# while end < l:
# heapq.heappush(heap_min, (nums[end], end))
# heapq.heappush(heap_max, (-1 * nums[end], end))
# while len(heap_min) > 0 and nums[end] - list(heap_min)[0][0] > limit:
# value, idx = heapq.heappop(heap_min)
# begin = max(begin, idx + 1)
# while len(heap_max) > 0 and (list(heap_max)[0][0]*-1) - nums[end] > limit:
# value, idx = heapq.heappop(heap_max)
# begin = max(begin, idx + 1)
# if end - begin + 1 > d:
# d = end - begin + 1
# end += 1
# return d
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
p1, p2, ans, cnt, min_, max_ = 0, 0, 0, {nums[0]: 1}, nums[0], nums[0]
while True:
if max_ - min_ <= limit:
ans = max(ans, p2-p1+1)
if p2 == len(nums)-1:
return ans
p2 += 1
if nums[p2] in cnt:
cnt[nums[p2]] += 1
else:
cnt[nums[p2]] = 1
max_ = max(max_, nums[p2])
min_ = min(min_, nums[p2])
else:
num = nums[p1]
p1 += 1
if cnt[num] == 1:
del cnt[num]
if max_ == num:
max_ = max(cnt.keys())
if min_ == num:
min_ = min(cnt.keys())
else:
cnt[num] -= 1
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums, limit):
res, last = 0, -1
maxQ, minQ = deque(), deque()
for i, val in enumerate(nums):
while maxQ and nums[maxQ[0]] - val > limit: # decrease
last = max(last, maxQ.popleft())
while maxQ and val >= nums[maxQ[-1]]:
maxQ.pop()
maxQ.append(i)
while minQ and val - nums[minQ[0]] > limit: # increase
last = max(last, minQ.popleft())
while minQ and val <= nums[minQ[-1]]:
minQ.pop()
minQ.append(i)
res = max(res, i - last)
return res
class Solution:
def longestSubarray(self, A, limit):
maxd = collections.deque()
mind = collections.deque()
i = 0
for a in A:
while len(maxd) and a > maxd[-1]: maxd.pop()
while len(mind) and a < mind[-1]: mind.pop()
maxd.append(a)
mind.append(a)
if maxd[0] - mind[0] > limit:
if maxd[0] == A[i]: maxd.popleft()
if mind[0] == A[i]: mind.popleft()
i += 1
return len(A) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if(not(nums)):
return 0
if(len(nums) == 1):
return 1
#n = len(nums)
start = 0
end = 1
maxi = nums[0]
mini = nums[0]
max_l = 1
while(start <= end and end < len(nums)):
maxi = max(maxi, nums[end])
mini = min(mini, nums[end])
if(maxi - mini <= limit):
max_l = max(max_l, end-start+1)
else:
#max_l = max(max_l, end-start)
if(nums[start] == maxi):
maxi = max(nums[start+1:end+1])
if(nums[start] == mini):
mini = min(nums[start+1:end+1])
start += 1
end += 1
return max_l
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
left=0
n=len(nums)
l=0
r=1
maxVal=nums[0]
minVal=nums[0]
res=1
while(l<r and r<n):
maxVal=max(maxVal,nums[r])
minVal=min(minVal,nums[r])
if(maxVal-minVal<=limit):
res=max(res,r-l+1)
else:
if(nums[l]==maxVal):
maxVal=max(nums[l+1:r+1])
if(nums[l]==minVal):
minVal=min(nums[l+1:r+1])
l+=1
r+=1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
l = 0
r = 1
curr_max = nums[0]
curr_min = nums[0]
max_len = 1
while l <= r and r < len(nums):
curr_max = max(curr_max, nums[r])
curr_min = min(curr_min, nums[r])
if curr_max - curr_min <= limit:
max_len = max(max_len, r - l + 1)
else:
if curr_max == nums[l]:
curr_max = max(nums[l+1:r+1])
if curr_min == nums[l]:
curr_min = min(nums[l+1:r+1])
l+=1
r+=1
return max_len
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_h = [(nums[0], 0)]
max_h = [(-nums[0], 0)]
heapq.heapify(min_h)
heapq.heapify(max_h)
max_v, min_v = nums[0], nums[0]
l, r = 0, 0
res = 1
while r < len(nums):
if max_v - min_v <= limit:
r += 1
if r < len(nums):
heapq.heappush(min_h, (nums[r], r))
heapq.heappush(max_h, (-nums[r], r))
max_v, min_v = -max_h[0][0], min_h[0][0]
else:
res = max(res, r-l)
l += 1
while min_h[0][1] < l:
heapq.heappop(min_h)
while max_h[0][1] < l:
heapq.heappop(max_h)
max_v, min_v = -max_h[0][0], min_h[0][0]
return max(res, r-l)
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_q = deque()
min_q = deque()
i = 0
res = 0
for j, num in enumerate(nums):
while max_q and num > max_q[-1]:
max_q.pop()
while min_q and num < min_q[-1]:
min_q.pop()
max_q.append(num)
min_q.append(num)
while max_q[0] - min_q[0] > limit:
if max_q[0] == nums[i]:
max_q.popleft()
if min_q[0] == nums[i]:
min_q.popleft()
i += 1
res = max(res, j - i + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
start = 0
end = 0
longest_size = 0
maxa = nums[0]
mina = nums[0]
while end < len(nums):
if maxa - mina <= limit:
longest_size = max(longest_size, end-start)
maxa = max(maxa, nums[end])
mina = min(mina, nums[end])
end += 1
else:
remove = nums[start]
start += 1
if remove == mina:
while nums[start] <= mina:
start += 1
mina = min(nums[start:end+1])
if remove == maxa:
while nums[start] >= maxa:
start += 1
maxa = max(nums[start:end+1])
if maxa-mina <= limit:
longest_size = max(longest_size, end-start)
return longest_size
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
max_stack = collections.deque([0]) # Used to track maximum number
min_stack = collections.deque([0]) # Used to track minimum number
begin, end, longest = 0, 0, 0
while end < n:
while True:
diff = nums[max_stack[0]] - nums[min_stack[0]]
if diff > limit:
if min_stack[0] == begin:
min_stack.popleft()
if max_stack[0] == begin:
max_stack.popleft()
begin += 1
else:
longest = max(longest, end - begin + 1)
break
end += 1
if end < n:
while min_stack and nums[end] <= nums[min_stack[-1]]:
min_stack.pop()
min_stack.append(end)
while max_stack and nums[end] >= nums[max_stack[-1]]:
max_stack.pop()
max_stack.append(end)
return longest
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A, limit):
maxd = collections.deque()
mind = collections.deque()
i = 0
for a in A:
while len(maxd) and a > maxd[-1]: maxd.pop()
while len(mind) and a < mind[-1]: mind.pop()
maxd.append(a)
mind.append(a)
if maxd[0] - mind[0] > limit:
if maxd[0] == A[i]: maxd.popleft()
if mind[0] == A[i]: mind.popleft()
i += 1
return len(A) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums or len(nums) == 1:
return len(nums)
n = len(nums)
i = 0
j = 1
res = 1
min_in_window = nums[0]
max_in_window = nums[0]
while j < n:
max_in_window = max(max_in_window, nums[j])
min_in_window = min(min_in_window, nums[j])
diff = abs(min_in_window - max_in_window)
if diff <= limit:
j += 1
res = max(res, j - i)
else:
tmp = nums[i]
while i <= j and i < n - 1 and nums[i] == nums[i+1]:
i += 1
if nums[i] == min_in_window:
min_in_window = min(nums[i+1:j+1])
if nums[i] == max_in_window:
max_in_window = max(nums[i+1:j+1])
i += 1
j += 1
return res
# if not nums:
# return 0
# if len(nums) == 1:
# return 1
# l, r = 0, 1
# res = 1
# min_in_window = nums[0]
# max_in_window = nums[0]
# while r < len(nums):
# min_in_window = min(min_in_window, nums[r])
# max_in_window = max(max_in_window, nums[r])
# if abs(min_in_window - max_in_window) <= limit:
# res = max(res, r - l + 1)
# else:
# while l <= r and l < len(nums) - 1 and nums[l] == nums[l+1]:
# l += 1
# if nums[l] == min_in_window:
# min_in_window = min(nums[l+1:r+1])
# if nums[l] == max_in_window:
# max_in_window = max(nums[l+1:r+1])
# l += 1
# r += 1
# return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_len, start, end = 1, 0, 0
pqmin, pqmax = [], []
for end in range(len(nums)):
heapq.heappush(pqmin, (nums[end], end))
heapq.heappush(pqmax, (-nums[end], end))
while start < end and abs(pqmax[0][0] + pqmin[0][0]) > limit:
start += 1
while pqmax and pqmax[0][1] < start: heapq.heappop(pqmax)
while pqmin and pqmin[0][1] < start: heapq.heappop(pqmin)
max_len = max(max_len, end - start + 1)
return max_len
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minheap=[]
maxheap=[]
heapq.heapify(minheap)
heapq.heapify(maxheap)
length=1
i=0
j=1
heapq.heappush(minheap,[nums[0],0]) # add first element and its index
heapq.heappush(maxheap,[-nums[0],0])
while j < len(nums):
if nums[j]-minheap[0][0]<=limit and maxheap[0][0]*-1-nums[j]<= limit:
length=max(length,j-i+1)
heapq.heappush(minheap,[nums[j],j])
heapq.heappush(maxheap,[-nums[j],j])
j=j+1
else:
while len(minheap)>0 and abs(minheap[0][0]-nums[j])>limit:
ele,index=heapq.heappop(minheap)
i=max(i,index + 1)
while len(maxheap)>0 and abs(-maxheap[0][0]-nums[j])>limit:
ele,index=heapq.heappop(maxheap)
i=max(i,index + 1)
# i=maxindex+1 # update i and now we are not concerned with element before ith index
heapq.heappush(minheap,[nums[j],j]) # add element and its index
heapq.heappush(maxheap,[-nums[j],j])
j=j+1
return length
# begin = end = 0
# l = len(nums)
# d = 0
# heap_min = []
# heap_max = []
# heapq.heapify(heap_min)
# heapq.heapify(heap_max)
# while end < l:
# heapq.heappush(heap_min, (nums[end], end))
# heapq.heappush(heap_max, (-1 * nums[end], end))
# while len(heap_min) > 0 and nums[end] - list(heap_min)[0][0] > limit:
# value, idx = heapq.heappop(heap_min)
# begin = max(begin, idx + 1)
# while len(heap_max) > 0 and (list(heap_max)[0][0]*-1) - nums[end] > limit:
# value, idx = heapq.heappop(heap_max)
# begin = max(begin, idx + 1)
# if end - begin + 1 > d:
# d = end - begin + 1
# end += 1
# return d
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A, limit):
maxd = collections.deque()
mind = collections.deque()
i = 0
for a in A:
while len(maxd) and a > maxd[-1]:
maxd.pop()
while len(mind) and a < mind[-1]:
mind.pop()
maxd.append(a)
mind.append(a)
if maxd[0] - mind[0] > limit:
if maxd[0] == A[i]:
maxd.popleft()
if mind[0] == A[i]:
mind.popleft()
i += 1
return len(A) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
answer = 1
counts = {nums[0] : 1}
subarray = deque([nums[0]])
max_val = nums[0]
min_val = nums[0]
for i in range(1, len(nums)):
counts[nums[i]] = counts.get(nums[i], 0) + 1
max_val = max(max_val, nums[i])
min_val = min(min_val, nums[i])
subarray.append(nums[i])
if abs(max_val - min_val) > limit:
while subarray:
num = subarray.popleft()
counts[num] -= 1
if counts[num] == 0:
if max_val == num:
if subarray:
max_val = max(subarray)
else:
max_val = 0
break
elif min_val == num:
if subarray:
min_val = min(subarray)
else:
min_val = 10 ** 9 + 1
break
answer = max(answer, len(subarray))
return answer
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
i=0
j=1
n=len(nums)
if n==0:
return 0
mn=nums[0]
mx=nums[0]
l=1
while i<=j and j<n:
if mn>nums[j]:
mn=nums[j]
if mx<nums[j]:
mx=nums[j]
#print(i,j,mn,mx)
if mx-mn<=limit:
if j-i+1>l:
l=j-i+1
else:
if mn==nums[i]:
mn=min(nums[i+1:j+1])
if mx==nums[i]:
mx=max(nums[i+1:j+1])
i+=1
j+=1
return l
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
'''def longestSubarray(self, nums: List[int], limit: int) -> int:
# TC is O(N^2)
start = 0
L = []
maxlen = 0
for end, item in enumerate(nums):
# insert the current item in appropriate position
bisect.insort(L, item) # insort is O(N)
# if the difference between start and end is more than expected, keep popping the start indexes
while L[-1] - L[0] > limit:
#*****bisect will give you the index but it is ALWAYS +1
idx = bisect.bisect(L, nums[start]) - 1 # logN TC
L.pop(idx) # pop that shit and recompute max-min difference again
start += 1
maxlen = max(maxlen, end-start+1)
return maxlen'''
def longestSubarray(self, nums: List[int], limit: int) -> int:
# insort was expesive in above O(N) so use 2 heaps to maintain max and min
maxheap = []
minheap = []
start = 0
maxlen = 0
for end, item in enumerate(nums):
heapq.heappush(minheap, (item, end))
heapq.heappush(maxheap, (-item, end))
#print(minheap, maxheap)
while maxheap and minheap and -maxheap[0][0]-minheap[0][0]>limit:
#print()
#print('heap', minheap, maxheap)
# find the minimum index of max or min
start = min(maxheap[0][1], minheap[0][1]) + 1
# it will be either of min or max, make sure to pop shit just above start so we dont have the max and min item below start
while maxheap[0][1] < start:
heapq.heappop(maxheap)
while minheap[0][1] < start:
heapq.heappop(minheap)
#print('heap', minheap, maxheap)
maxlen = max(maxlen, end-start+1)
return maxlen
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
res = 0
left, right = 0, 0
inc, dec = collections.deque(), collections.deque()
while right < n:
while inc and nums[inc[-1]] >= nums[right]: # 允许相同元素存在,这样的话滑动窗口pop的时候才能正确的pop完,否则会出现过早的pop完但left还没跟上的情况
inc.pop()
inc.append(right)
while dec and nums[dec[-1]] <= nums[right]:
dec.pop()
dec.append(right)
right += 1
while nums[dec[0]] - nums[inc[0]] > limit:
if inc[0] == left:
inc.popleft()
if dec[0] == left:
dec.popleft()
left += 1
res = max(res, right - left)
# print(left, right, res, inc, dec, )
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# asc stack
# desc stack
asc, desc = deque(), deque()
left = 0
longest = 0
for i, n in enumerate(nums):
if not asc:
asc.append([n, i])
else:
while asc and asc[-1][0] > n:
asc.pop()
asc.append([n, i])
if not desc:
desc.append([n, i])
else:
while desc and desc[-1][0] < n:
desc.pop()
desc.append([n, i])
while desc[0][0] - asc[0][0] > limit:
if desc[0][1] < asc[0][1]:
left = desc.popleft()[1] + 1
else:
left = asc.popleft()[1] + 1
longest = max(longest, i-left+1)
return longest
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums, limit):
_min = _max = nums[0]
count = 0
res, start =1, 0
for right in range(len(nums)):
if _min>nums[right]: _min = nums[right]
if _max<nums[right]: _max = nums[right]
if _max-_min <= limit:
count += 1
if res < count: res = count
else:
_min=_max=nums[right]
count = 1
left = right - 1
while (left>=start and abs(_max - nums[left])<=limit
and abs(_min - nums[left]) <= limit):
if _min>nums[left]: _min = nums[left]
if _max<nums[left]: _max = nums[left]
count += 1
left -= 1
start = right+1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A, limit):
maxd = collections.deque()
mind = collections.deque()
i = 0
for a in A:
while len(maxd) and a > maxd[-1]: maxd.pop()
while len(mind) and a < mind[-1]: mind.pop()
maxd.append(a)
mind.append(a)
if maxd[0] - mind[0] > limit:
if maxd[0] == A[i]: maxd.popleft()
if mind[0] == A[i]: mind.popleft()
i += 1
return len(A) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
l = r = 0
minq = collections.deque()
maxq = collections.deque()
ans = 0
while r < len(nums):
while minq and nums[minq[-1]] >= nums[r]:
minq.pop()
while maxq and nums[maxq[-1]] <= nums[r]:
maxq.pop()
minq.append(r)
maxq.append(r)
while nums[maxq[0]] - nums[minq[0]] > limit:
l += 1
if l > minq[0]:
minq.popleft()
if l > maxq[0]:
maxq.popleft()
ans = max(ans, r - l + 1)
r += 1
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums)==1:
return 1
#nums.sort()
#nums = [8,2,4,7]
maxL=0
beg=0
#end=1
'''
while beg<len(nums)-1:
#mx=max(nums[beg:(end+1)])
#mn=min(nums[beg:(end+1)])
while end<len(nums) and max(nums[beg:(end+1)])-min(nums[beg:(end+1)])<=limit:
maxL=max(maxL, end-beg+1)
end+=1
beg+=1
end=beg+1
return maxL
'''
minQueue, maxQueue = collections.deque([]), collections.deque([])
#minQueue, maxQueue=[], []
#minQueue increasing, maxQueue decreasing
#minQueue.append(nums[beg])
#maxQueue.append(nums[beg])
end=beg
while end<len(nums):
#need while loop because if minQueue=[3,4,7], nums[end]=2
#then since minQueue must be increasing and need to append nums[end]=2
#so must remove 3,4, and 7
while len(minQueue)>0 and nums[end]<minQueue[-1]:
minQueue.pop()
minQueue.append(nums[end]) #[2]
#print(\" min: \", minQueue)
while len(maxQueue)>0 and nums[end]>maxQueue[-1]:
maxQueue.pop()
maxQueue.append(nums[end]) #[7,2]
if maxQueue[0]-minQueue[0]<=limit:
maxL=max(maxL, end-beg+1)
else:
if maxQueue[0]==nums[beg]:
maxQueue.popleft() #[1]
#maxQueue.pop(0) #[1]
if minQueue[0]==nums[beg]:
minQueue.popleft() #[1]
#minQueue.pop(0) #[1]
beg+=1
end+=1
return maxL
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque = collections.deque()
max_deque = collections.deque()
left = 0
right = 0
max_size = 0
while right < len(nums):
while min_deque and nums[min_deque[-1]] >= nums[right]:
min_deque.pop()
while max_deque and nums[max_deque[-1]] <= nums[right]:
max_deque.pop()
min_deque.append(right)
max_deque.append(right)
while nums[max_deque[0]] - nums[min_deque[0]] > limit:
left += 1
if max_deque[0] < left:
max_deque.popleft()
if min_deque[0] < left:
min_deque.popleft()
right += 1
max_size = max(max_size, right - left)
return max_size
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
import collections
min_deque = deque()
max_deque = deque()
l = 0
r = 0
ans = 0
while r < len(nums):
while min_deque and nums[r] <= nums[min_deque[-1]]:
min_deque.pop()
while max_deque and nums[r] >= nums[max_deque[-1]]:
max_deque.pop()
min_deque.append(r)
max_deque.append(r)
while nums[max_deque[0]] - nums[min_deque[0]] > limit:
l += 1
if l > min_deque[0]:
min_deque.popleft()
if l > max_deque[0]:
max_deque.popleft()
ans = max(ans, r - l + 1)
r += 1
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
# import collections
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq, minq = [], []
res = i = 0
for j in range(len(nums)):
heapq.heappush(maxq, (-nums[j], j))
heapq.heappush(minq, (nums[j], j))
while -maxq[0][0] - minq[0][0] > limit:
i = min(maxq[0][1], minq[0][1]) + 1
while i > maxq[0][1]:
heapq.heappop(maxq)
while i > minq[0][1]:
heapq.heappop(minq)
res = max(res, j-i+1)
return res
# maxq = collections.deque()
# minq = collections.deque()
# res = i = 0
# for j in range(len(nums)):
# while maxq and nums[maxq[-1]] < nums[j]: maxq.pop()
# while minq and nums[minq[-1]] > nums[j]: minq.pop()
# maxq.append(j)
# minq.append(j)
# while nums[maxq[0]] - nums[minq[0]] > limit:
# i = min(maxq[0], minq[0]) + 1
# while i > maxq[0]: maxq.popleft()
# while i > minq[0]: minq.popleft()
# res = max(res, j-i+1)
# return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from heapq import heappush,heappop
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
i = j = best = 0
minHeap, maxHeap = [], []
while j < len(nums):
heappush(minHeap,(nums[j],j))
heappush(maxHeap,(-nums[j],j))
while abs(nums[j] + maxHeap[0][0]) > limit:
i = max(i, heappop(maxHeap)[1] + 1)
while abs(nums[j] - minHeap[0][0]) > limit:
i = max(i, heappop(minHeap)[1] + 1)
j+= 1
best = max(best, j - i)
return best
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import collections
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minq = collections.deque()
maxq = collections.deque()
j = 0
count = 0
for i, val in enumerate(nums):
while len(minq) > 0 and minq[-1] > val:
minq.pop()
while len(maxq) > 0 and maxq[-1] < val:
maxq.pop()
minq.append(val)
maxq.append(val)
while len(minq) > 0 and len(maxq) > 0 and maxq[0] - minq[0] > limit:
if nums[j] == maxq[0]:
maxq.popleft()
if nums[j] == minq[0]:
minq.popleft()
j += 1
count = max(count, i - j + 1)
return count
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxd = collections.deque()
mind = collections.deque()
i = 0
for a in nums:
while len(maxd) and a > maxd[-1]: maxd.pop()
while len(mind) and a < mind[-1]: mind.pop()
maxd.append(a)
mind.append(a)
if maxd[0] - mind[0] > limit:
if maxd[0] == nums[i]: maxd.popleft()
if mind[0] == nums[i]: mind.popleft()
i += 1
return len(nums) - i
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums or limit < 0:
return 0
anchor = ans = 0
hi, lo = float('-inf'), float('inf')
for i in range(len(nums)):
hi = max(hi, nums[i])
lo = min(lo, nums[i])
if (hi - lo) <= limit:
ans = max(ans, i - anchor + 1)
else:
if nums[anchor] == hi:
hi = max(nums[anchor + 1 : i + 1])
elif nums[anchor] == lo:
lo = min(nums[anchor + 1 : i + 1])
anchor += 1
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
if len(nums) == 1:
return 1
l = 0
r = 1
cur_mx = nums[0]
cur_mn = nums[0]
max_l = 1
while l <= r and r < len(nums):
cur_mx = max(cur_mx, nums[r])
cur_mn = min(cur_mn, nums[r])
if cur_mx - cur_mn <= limit:
max_l = max(max_l, r - l + 1)
else:
if nums[l] == cur_mx:
cur_mx = max(nums[l + 1:r + 1])
if nums[l] == cur_mn:
cur_mn = min(nums[l + 1:r + 1])
l += 1
r += 1
return max_l
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
if len(nums) == 1:
return 1
cur_max = nums[0]
cur_min = nums[0]
l=0
r=1
max_l = 1
while l<=r and r<len(nums):
cur_max = max(cur_max, nums[r])
cur_min = min(cur_min, nums[r])
if cur_max-cur_min <=limit:
max_l = max(max_l, r-l+1)
else:
if nums[l] == cur_max:
cur_max = max(nums[l+1:r+1])
if nums[l] == cur_min:
cur_min = min(nums[l+1:r+1])
l+=1
r+=1
return max_l
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from sortedcontainers import SortedList
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# we keep two pointers and we keep track of min and max between those pointers
# if the new number being added is having difference from both min and max within the range we increase j and update min and max if necessary else we increase i till we get the difference less than target while changing i and also we keep on updating the min and max
# it can mark the beginning of a new sub array
# however if we can make this update faster constant time and at max we need to increase i from i to j by deleting each number then also it is in worse case O(nk) instead of linear we can save sometime
# we do not need indices and so we can use SortedList instead of SortedDict to make processing of finding min and max after changing i faster
minheap = [[nums[0],0]]
maxheap = [[-nums[0],0]]
left = -1
res = 1
for i,val in enumerate(nums[1:]):
j = i+1
while len(maxheap) and len(minheap) and max(val,-maxheap[0][0]) - min(val,minheap[0][0]) > limit:
if val == max(val,-maxheap[0][0]):
v,l = heapq.heappop(minheap)
if minheap and val == min(val,minheap[0][0]):
v,l = heapq.heappop(maxheap)
left = max(l,left)
res = max(j-left,res)
heapq.heappush(minheap,[val,j])
heapq.heappush(maxheap,[-val,j])
return res
'''
s = SortedList()
# we keep on adding new values to the sortedList and when we get max - min greater than target then we keep on removing values from the list till the sorted list becomes valid again
# thus we have a solution with O(nlogn) complexity and faster than the one with sortedDict although it is having same complexity
max_length = 0
j = 0
for i in range(len(nums)):
s.add(nums[i])
while s and s[-1] - s[0] > limit:
s.remove(nums[j])
j += 1
max_length = max(max_length,i-j + 1)
return max_length
'''
# for the following approach also the time limit gets exceeded because although operations on dictionary are O(logn) we are doing them for more number of times then expected
'''
# i.e. if min is our problem then we need to increase i till we find new min
# i.e we need to start from i = i + max_index of current_min
# and if max is our problem then we need to increase i till we find new max
# i.e we need to start from i = i + max_index of current_max
# and if both are a problem then we need to increase i till max when both are changed
# this we can do if we use a hashmap mapping each value from i to j with its max index in the array we do not care for intermediate indices
max_length = -float('inf')
mapping = SortedDict()
Min = nums[0]
Max = nums[0]
mapping[Min] = 0
i = 0
while i < len(nums):
j = i
# print('a',i,j,Max,Min)
while j < len(nums) and abs(nums[j] - Min) <= limit and abs(nums[j] - Max) <= limit:
Max = max(Max,nums[j])
Min = min(Min,nums[j])
# we are just concerned with the max value
mapping[nums[j]] = j
j += 1
max_length = max(max_length,j - i)
# print('b',i,j,Min,Max)
if j >= len(nums)-1:
break
else:
mapping[nums[j]] = j
while i <= j and (abs(nums[j] - Min) > limit or abs(nums[j] - Max) > limit):
if abs(nums[j] - Min) > limit and abs(nums[j] - Max) > limit:
i = max(i,max(mapping[Min],mapping[Max]) + 1)
del mapping[Max]
if Min in mapping.keys():
del mapping[Min]
elif abs(nums[j] - Min) > limit:
i = max(i,mapping[Min] + 1)
del mapping[Min]
elif abs(nums[j] - Max) > limit:
i = max(i,mapping[Max] + 1)
del mapping[Max]
# after each update we need to update min and max and the following steps will take O(n)
Min = mapping.peekitem(0)[0]
Max = mapping.peekitem()[0]
while mapping[Min] < i:
del mapping[Min]
Min = mapping.peekitem(0)[0]
while mapping[Max] < i:
del mapping[Max]
Max = mapping.peekitem()[0]
# print(mapping)
# print('c',i,j,Max,Min)
return max_length
'''
# for the following approach time limit gets exceeded
'''
max_length = -float('inf')
Min = nums[0]
Max = nums[0]
i = 0
while i < len(nums):
j = i
#print('a',i,j,Max,Min)
while j < len(nums) and abs(nums[j] - Min) <= limit and abs(nums[j] - Max) <= limit:
Max = max(Max,nums[j])
Min = min(Min,nums[j])
j += 1
max_length = max(max_length,j - i)
#print('b',i,j,Max,Min,len(nums))
if j >= len(nums)-1:
break
else:
while i < j and nums[i] != Min and nums[i] != Max:
i += 1
while i < j and (abs(nums[j] - Min) > limit or abs(nums[j] - Max) > limit):
i += 1
#print(i)
Min = min(nums[i:j+1])
Max = max(nums[i:j+1])
#print('c',i,j,Max,Min)
return max_length
'''
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxLen = 1
curMax = curMin = nums[0]
l = r = 0
while r < len(nums):
curMax = max(curMax, nums[r])
curMin = min(curMin, nums[r])
if curMax - curMin <= limit:
maxLen = max(maxLen, r-l+1)
else:
if nums[l] == curMin:
curMin = min(nums[l+1:r+1])
if nums[l] == curMax:
curMax = max(nums[l+1:r+1])
l += 1
r += 1
return maxLen
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import heapq
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
'''
Find the longest non-empty subarray s where max(s) - min(s) <= limit
'''
max_q, min_q = [[nums[0], 0]], [[nums[0], 0]]
ans = 1
i = j = 0
while j < len(nums):
heapq.heappush(max_q, [-nums[j], j])
heapq.heappush(min_q, [nums[j], j])
if -max_q[0][0] - min_q[0][0] <= limit:
ans = max(ans, j-i+1)
j += 1
else:
# Fast forward i
i = min(max_q[0][1], min_q[0][1]) + 1
while max_q[0][1] < i: heapq.heappop(max_q)
while min_q[0][1] < i: heapq.heappop(min_q)
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
if len(nums) == 1:
return 1
left = 0
right = 0
curr_min = nums[0]
curr_max = nums[0]
max_len = 1
while left <= right and right < len(nums):
curr_max = max(curr_max, nums[right])
curr_min = min(curr_min, nums[right])
if curr_max - curr_min <= limit:
max_len = max(max_len, right - left + 1)
else:
if nums[left] == curr_max:
curr_max = max(nums[left + 1: right + 1])
if nums[left] == curr_min:
curr_min = min(nums[left + 1: right + 1])
left += 1
right += 1
return max_len
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
if n == 0:
return 0
if n == 1:
return 1
ans = 1
minelement = maxelement = nums[0]
l = 0
r = 1
while (r<n and l<=r):
minelement = min(minelement, nums[r])
maxelement = max(maxelement, nums[r])
if maxelement-minelement <= limit:
ans = max(ans, r-l+1)
else:
# recompute min and max
if minelement == nums[l]:
minelement = min(nums[l+1: r+1])
if maxelement == nums[l]:
maxelement = max(nums[l+1: r+1])
l += 1
r += 1
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if (len(nums)==0):
return 0
if(len(nums)==1):
return 1
ans = 1
minNum = nums[0]
maxNum = nums[0]
i=0
j=0
n = len(nums)
while i<n and j<n:
maxNum = max(maxNum, nums[i])
minNum = min(minNum, nums[i])
if(abs(maxNum - minNum) > limit):
if(nums[j] == maxNum):
maxNum = max(nums[j+1:i+1])
elif(nums[j] == minNum):
minNum = min(nums[j+1:i+1])
j+=1
else:
ans = max(ans, i-j+1)
i+=1
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from collections import deque
class Solution:
def longestSubarray(self, nums, limit):
res, last = 0, -1
maxQ, minQ = deque(), deque()
for i, val in enumerate(nums):
while maxQ and nums[maxQ[0]] - val > limit:
last = max(last, maxQ.popleft())
while minQ and val - nums[minQ[0]] > limit:
last = max(last, minQ.popleft())
res = max(res, i - last)
while maxQ and val >= nums[maxQ[-1]]:
maxQ.pop()
maxQ.append(i)
while minQ and val <= nums[minQ[-1]]:
minQ.pop()
minQ.append(i)
return res
from collections import deque
class Solution:
def longestSubarray(self, nums, limit):
res, last = 0, -1
maxQ, minQ = deque(), deque()
for i, val in enumerate(nums):
while maxQ and nums[maxQ[0]] - val > limit:
last = max(last, maxQ.popleft())
while minQ and val - nums[minQ[0]] > limit:
last = max(last, minQ.popleft())
#res = max(res, i - last)
if i - last > res: res = i - last
while maxQ and val >= nums[maxQ[-1]]:
maxQ.pop()
maxQ.append(i)
while minQ and val <= nums[minQ[-1]]:
minQ.pop()
minQ.append(i)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
l, r = 0 , 0
n = len(nums)
ll = 0
mmin = nums[0]
mmax = nums[0]
while l <= r and r < n:
mmax = max(mmax, nums[r])
mmin = min(mmin, nums[r])
if abs(mmax - mmin) <= limit:
ll = max(ll, r -l + 1)
else:
if nums[l] == mmax:
mmax = max(nums[l+1: r+1])
if nums[l] == mmin:
mmin = min(nums[l+1:r+1])
l += 1
r += 1
return ll
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
currmax = currmin = nums[0]
start = 0
res = 1
for i in range(1,len(nums)):
currmax = max(currmax,nums[i])
currmin = min(currmin,nums[i])
print(currmax,currmin)
if currmax - currmin <= limit:
res = max(res,i-start+1)
else:
if currmax == nums[start]:
currmax = max(nums[start+1:i+1])
if currmin == nums[start]:
currmin = min(nums[start+1: i+1])
start += 1
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
smallest = largest = nums[0]
start, end, window = 0, 1, 1
while end < len(nums):
smallest, largest = min(smallest, nums[end]), max(largest, nums[end])
if largest - smallest <= limit:
window = max(window, end - start + 1)
else:
if nums[start] == smallest:
smallest = min(nums[start+1:end+1])
if nums[start] == largest:
largest = max(nums[start+1:end+1])
start += 1
end += 1
return window
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
import collections
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque = collections.deque()
max_deque = collections.deque()
longest_subarray = 0
i, j = 0, 0
min_deque.append((nums[0], 0))
max_deque.append((-nums[0], 0))
while j < len(nums) and len(nums)-i+1 > longest_subarray:
# Remove elements from the min and max that are stale
while min_deque and min_deque[0][1] < i: min_deque.popleft()
while max_deque and max_deque[0][1] < i: max_deque.popleft()
# Keep adding elements if the invariant is true
while i > j or (j < len(nums)-1 and (max(-max_deque[0][0], nums[j+1]) - min(min_deque[0][0], nums[j+1]) <= limit)):
j += 1
self.insert_to_deque(min_deque, nums[j], j)
self.insert_to_deque(max_deque, -nums[j], j)
longest_subarray = max(longest_subarray, j-i+1)
i += 1
return longest_subarray
def insert_to_deque(self, deque_object, value, index):
# Keep popping from the right until the new value is smaller
while len(deque_object) > 0 and deque_object[-1][0] >= value:
deque_object.pop()
deque_object.append((value, index))
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
currMax = nums[ 0 ]
currMin = nums[ 0 ]
maxSize = 1
beg = 0
end = 0
while end < len( nums ):
currMax = max( currMax, nums[ end ] )
currMin = min( currMin, nums[ end ] )
if currMax - currMin > limit:
beg += 1
if currMax == nums[ beg - 1 ]:
while currMax == nums[ beg ]:
beg += 1
currMax = max( nums[ beg : end + 1 ] )
if currMin == nums[ beg - 1 ]:
while currMin == nums[ beg ]:
beg += 1
currMin = min( nums[ beg : end + 1 ] )
else:
end += 1
maxSize = max( maxSize, end - beg )
return maxSize
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
min_heap, max_heap = [], []
res, i = 0, 0
for j, num in enumerate(nums):
heapq.heappush(min_heap, [num, j])
heapq.heappush(max_heap, [-num, j])
while abs(min_heap[0][0]+max_heap[0][0]) > limit:
i = min(min_heap[0][1], max_heap[0][1]) + 1
while min_heap[0][1] < i:
heapq.heappop(min_heap)
while max_heap[0][1] < i:
heapq.heappop(max_heap)
res = max(res, j-i+1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from heapq import heappush, heappop
from collections import deque
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# min_stack = []
# max_stack = []
# for i, number in enumerate(nums):
# _index = i
# while max_stack and number > max_stack[-1][0]:
# _, _index = max_stack.pop()
# max_stack.append((number, _index))
# _index = i
# while min_stack and number < min_stack[-1][0]:
# _, _index = min_stack.pop()
# min_stack.append((number, _index))
# longest = -1
# start_index = len(nums) - 1
# print(max_stack)
# print(min_stack)
# while max_stack[-1][0] - min_stack[-1][0] <= limit:
# longest = max(
# longest,
# abs(max_stack[-1][1] - min_stack[-1][1])
# )
# if max_stack[-1][1] > min_stack[-1][1]:
# max_stack.pop()
# else:
# min_stack.pop()
# return longest
left = 0
min_q = deque()
max_q = deque()
longest = 0
for right, number in enumerate(nums):
_index = right
while max_q and number > max_q[-1][0]:
_, _index = max_q.pop()
max_q.append((number, _index))
_index = right
while min_q and number <= min_q[-1][0]:
_, _index =min_q.pop()
min_q.append((number, _index))
while max_q[0][0] - min_q[0][0] > limit:
if max_q[0][1] > min_q[0][1]:
if len(min_q) == 1:
q = max_q
else:
q = min_q
else:
if len(max_q) == 1:
q = min_q
else:
q = max_q
_, _index = q.popleft()
left = max(min_q[0][1], max_q[0][1])
longest = max(longest, right - left + 1)
return longest
def calc_diff(self, a, b):
return abs(a - b)
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
# def longestSubarray(self, nums: List[int], limit: int) -> int:
# min_h = [(nums[0], 0)]
# max_h = [(-nums[0], 0)]
# heapq.heapify(min_h)
# heapq.heapify(max_h)
# max_v, min_v = nums[0], nums[0]
# l, r = 0, 0
# res = 1
# while r < len(nums):
# if max_v - min_v <= limit:
# r += 1
# if r < len(nums):
# heapq.heappush(min_h, (nums[r], r))
# heapq.heappush(max_h, (-nums[r], r))
# max_v, min_v = -max_h[0][0], min_h[0][0]
# else:
# res = max(res, r-l)
# l += 1
# while min_h[0][1] < l:
# heapq.heappop(min_h)
# while max_h[0][1] < l:
# heapq.heappop(max_h)
# max_v, min_v = -max_h[0][0], min_h[0][0]
# return max(res, r-l)
def longestSubarray(self, nums, limit):
max_h, min_h = [], []
res = l = 0
for r, num in enumerate(nums):
heapq.heappush(max_h, (-num, r))
heapq.heappush(min_h, (num, r))
while -max_h[0][0] - min_h[0][0] > limit:
l += 1
while max_h[0][1] < l:
heapq.heappop(max_h)
while min_h[0][1] < l:
heapq.heappop(min_h)
res = max(res, r - l + 1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
# [8, 2, 4, 7] limit = 4
# want to keep track of max and min elements + indexes
# thinking of a sliding window approach
# 8, 8 -> 8, 2 -> 2, 4 -> 2, 7 -> end
# difference between i and j + 1
# [10, 1, 2, 4, 7, 2], limit = 5
# 10, 10 -> 10, 1 -> 1, 2 -> 1, 4 -> 1, 7 -> 2, 7 -> 2, 2 -> end
# turns out i need a list of max and min elements + indexes
# [8, 11, 5, 7, 9, 10, 8, 7, 6];
# (8, 11) -> (11, 5) ->
# The list of max elements must contain elements in window, descending order
# The list of min elements must contain elements in window, ascending order
# (6, 8) (7, 7) (9, 6)
# (9, 6)
# i = 3, j = 10
# res = 6
if limit < 0:
return 0
i, j = 0, 1
res = 0
max_q, min_q = collections.deque(), collections.deque()
max_q.append((0, nums[0]))
min_q.append((0, nums[0]))
while j < len(nums):
if (max_q[0][1] - min_q[0][1] <= limit):
while max_q and nums[j] > max_q[-1][1]:
max_q.pop()
while min_q and nums[j] < min_q[-1][1]:
min_q.pop()
max_q.append((j, nums[j]))
min_q.append((j, nums[j]))
j += 1
if (max_q[0][1] - min_q[0][1] > limit):
res = j - i - 1 if j - i - 1 > res else res
min_i, max_i = min_q[0][0], max_q[0][0]
if (min_i < max_i):
i = min_i + 1
min_q.popleft()
else:
i = max_i + 1
max_q.popleft()
return j - i if j - i > res else res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
def helper(arr, limit):
# 13
from collections import deque
Q = deque()
Q_2 = deque()
result = 0
if len(nums) == 1:
return 1
for i in range(len(arr)):
v = arr[i]
while len(Q) and abs(v - Q[0][1]) > limit:
j, _ = Q.popleft()
while len(Q_2) and abs(v - Q_2[0][1]) > limit:
j, _ = Q_2.popleft()
if len(Q) and len(Q_2):
r = i - max(Q[0][0], Q_2[0][0]) + 1
# print(r, Q, Q_2)
result = max(result, r)
ni = i
while len(Q) and v <= Q[-1][1]:
mp, vp = Q.pop() # <=============== change i !!!
if abs(vp - v) <= limit:
ni = mp
Q.append((ni, v))
ni = i
while len(Q_2) and v >= Q_2[-1][1]:
mp, vp = Q_2.pop() # <=============== change i !!!
if abs(vp - v) <= limit:
ni = mp
Q_2.append((ni, v))
return result
r1 = helper(nums, limit)
return r1
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums: return 0
pre = 0
res = 1
maxs = []
mins = []
for i in range(len(nums)):
while maxs and maxs[-1] < nums[i]:
maxs.pop()
while mins and mins[-1] > nums[i]:
mins.pop()
maxs.append(nums[i])
mins.append(nums[i])
while maxs[0] - mins[0] > limit:
if maxs[0] == nums[pre]: maxs.pop(0)
if mins[0] == nums[pre]: mins.pop(0)
pre += 1
res = max(res, i-pre+1)
return res
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
ans = 1
st = en = 0
min_q = collections.deque()
max_q = collections.deque()
while en < len(nums):
while min_q and nums[en] <= nums[min_q[-1]]:
min_q.pop()
while max_q and nums[en] >= nums[max_q[-1]]:
max_q.pop()
min_q.append(en)
max_q.append(en)
#print (st , en)
while nums[max_q[0]] - nums[min_q[0]] > limit:
st +=1
if st > min_q[0]:
min_q.popleft()
if st > max_q[0]:
max_q.popleft()
ans = max(ans, en - st + 1)
en +=1
#print(ans)
return ans
|
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
from heapq import heappush,heappop
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
i = j = best = 0
minHeap, maxHeap = [], []
while i < len(nums) and j < len(nums):
heappush(minHeap,(nums[j],j))
heappush(maxHeap,(-nums[j],j))
while abs(nums[j] + maxHeap[0][0]) > limit:
i = max(i, heappop(maxHeap)[1] + 1)
while abs(nums[j] - minHeap[0][0]) > limit:
i = max(i, heappop(minHeap)[1] + 1)
j+= 1
best = max(best, j - i)
return best
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.