Datasets:

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: min_deque = collections.deque() max_deque = collections.deque() l = 0 ans = 0 for r, num in enumerate(nums): while(min_deque and nums[min_deque[-1]] >= num): min_deque.pop() while(max_deque and nums[max_deque[-1]] <= num): max_deque.pop() min_deque.append(r) max_deque.append(r) while(nums[max_deque[0]] - nums[min_deque[0]] > limit): l += 1 while(max_deque[0] < l): max_deque.popleft() while(min_deque[0] < l): min_deque.popleft() ans = max(ans, r - l + 1) # print(\"iteration\") # print(r) # print(ans) # print(r - 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
import heapq class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: longest = 0 minHeap = [] maxHeap = [] front = -1 back = 0 while back < len(nums): needsShrinking = False if front < len(nums) - 1: front += 1 heapq.heappush(minHeap, (nums[front], front)) heapq.heappush(maxHeap, (-nums[front], front)) else: needsShrinking = True while needsShrinking or (minHeap and maxHeap and abs(minHeap[0][0] + maxHeap[0][0]) > limit): needsShrinking = False back += 1 while minHeap and minHeap[0][1] < back: heapq.heappop(minHeap) while maxHeap and maxHeap[0][1] < back: heapq.heappop(maxHeap) longest = max(longest, front - back + 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: 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
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: ret, vals = 0, SortedList() l, r, n = 0, 0, len(nums) while r < n: vals.add(nums[r]) while vals[-1] - vals[0] > limit: vals.remove(nums[l]) l += 1 r += 1 ret = max(ret, r - l) return ret
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: minhp=[] maxhp=[] i=0 ans=0 for j,el in enumerate(nums): heapq.heappush(minhp,[el,j]) heapq.heappush(maxhp,[-el,j]) while -maxhp[0][0]-minhp[0][0]>limit: i=min(maxhp[0][1],minhp[0][1])+1 while maxhp[0][1]<i: heapq.heappop(maxhp) while minhp[0][1]<i: heapq.heappop(minhp) ans=max(ans,j-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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: maxd = [] mind = [] left = 0 sol = 1 for i, num in enumerate(nums): while maxd and nums[maxd[-1]] < num: maxd.pop() while mind and nums[mind[-1]] > num: mind.pop() maxd.append(i) mind.append(i) while nums[maxd[0]] - nums[mind[0]] > limit: if nums[maxd[0]] == nums[left] : maxd.pop(0) if nums[mind[0]] == nums[left] : mind.pop(0) left += 1 sol = max(sol, i - left + 1) #print(maxd,mind,sol,left) return sol
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: mxh, mnh = [], [] i = j = 0 ans = 0 while i < len(nums): if j < len(nums) and (not mxh or not mnh or abs(mxh[0][0] + mnh[0][0]) <= limit): heappush(mnh, [nums[j], j]) heappush(mxh, [-nums[j], j]) j += 1 else: if (not mxh or not mnh or abs(mxh[0][0] + mnh[0][0]) <= limit): ans = max(ans, j - i) else: ans = max(ans, j - i - 1) i += 1 while mxh and mxh[0][1] < i: heappop(mxh) while mnh and mnh[0][1] < i: heappop(mnh) 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 MinQ: def __init__(self): self.Q = collections.deque([]) def append(self,num): cnt = 0 while self.Q and self.Q[-1][0] >= num: cnt += self.Q.pop()[1] + 1 self.Q.append([num,cnt]) def popleft(self): if not self.Q: return None if self.Q[0][1] == 0: self.Q.popleft() else: self.Q[0][1] -= 1 @property def minv(self): return self.Q[0][0] class MaxQ: def __init__(self): self.Q = collections.deque([]) def append(self,num): cnt = 0 while self.Q and self.Q[-1][0] <= num: cnt += self.Q.pop()[1] + 1 self.Q.append([num,cnt]) def popleft(self): if not self.Q: return None if self.Q[0][1] == 0: self.Q.popleft() else: self.Q[0][1] -= 1 @property def maxv(self): return self.Q[0][0] class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: n = len(nums) left = 0 res = 0 right = 0 maxq = MaxQ() minq = MinQ() while right < n: maxq.append(nums[right]) minq.append(nums[right]) while maxq.maxv - minq.minv > limit: maxq.popleft() minq.popleft() left += 1 res = max(right - left + 1, res) 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, nums: List[int], limit: int) -> int: # heap maps max_heap, min_heap = [], [] max_len = 0 l = 0 for r,n in enumerate(nums): heapq.heappush(max_heap, (-n,r)) heapq.heappush(min_heap, (n,r)) while -max_heap[0][0]-min_heap[0][0] > limit: l = min(max_heap[0][1], min_heap[0][1]) + 1 while max_heap[0][1] < l: heapq.heappop(max_heap) while min_heap[0][1] < l: heapq.heappop(min_heap) max_len = max(max_len, r-l+1) return max_len ''' # using heaps maxq, minq = [], [] n = len(nums) r = 0 for l in range(n): heapq.heappush(maxq, (-nums[i], l)) heapq.heappush(minq, (nums[i], l)) while abs(maxq[0][0] + minq[0][0]) <= limit: while max[0][0] '''
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: # minheap=[] # maxheap=[] # heapq.heapify(minheap) # heapq.heapify(maxheap) # heapq.heappush(minheap,[nums[0],0]) # add first element and its index # heapq.heappush(maxheap,[-nums[0],0]) # begin = 0 # length = 1 # end = 1 # next_idx = 0 # while begin <= end and end < len(nums) - 1: # if abs(minheap[0][0] - nums[end]) <= limit and abs(abs(maxheap[0][0]) - nums[end]) <= limit: # length = max(length, end - begin + 1) # heapq.heappush(minheap, [nums[end],end]) # heapq.heappush(maxheap, [-nums[end],end]) # end+=1 # else: # while len(minheap) and abs(minheap[0][0] - nums[end]) > limit: # ele,idx = heapq.heappop(minheap) # next_idx = max(next_idx, idx) # while len(maxheap) and abs(maxheap[0][0] - nums[end]) > limit: # ele,idx = heapq.heappop(maxheap) # next_idx = max(next_idx, idx) # begin = next_idx + 1 # heapq.heappush(minheap, [nums[end],end]) # heapq.heappush(maxheap, [-nums[end],end]) # end+=1 # return length 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]) maxindex=0 while i<=j and j<=len(nums)-1: 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) maxindex=max(maxindex,index) while len(maxheap)>0 and abs(-maxheap[0][0]-nums[j])>limit: ele,index=heapq.heappop(maxheap) maxindex=max(maxindex,index) 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 # def isValid(int_diff, j, max_value, min_value, limit, nums): # max_boundary = abs(max_value - nums[j]) # min_boundary = abs(min_value - nums[j]) # return int_diff <= limit and max_boundary <= limit and min_boundary <= limit # def find_error(int_diff,i, j, max_value, min_value, limit, nums,cache): # max_boundary = abs(max_value - nums[j]) # min_boundary = abs(min_value - nums[j]) # if int_diff > limit: return i # if max_boundary > limit: return cache[max_value] # if min_boundary > limit: return cache[min_value] # output = 1 # i = 0 # idx = 0 # cache = defaultdict(int) # while i < len(nums) and idx < len(nums)-1: # min_value = float('inf') # max_value = float('-inf') # cache[min_value] = i # cache[max_value] = i # for j in range(i,len(nums)): # int_diff = abs(nums[j] - nums[i]) # temp_min = min_value # temp_max = max_value # min_value = min(min_value, nums[j]) # max_value = max(max_value, nums[j]) # if temp_min != min_value: # cache[min_value] = j # if temp_max != min_value: # cache[max_value] = j # if isValid(int_diff, j, max_value, min_value, limit, nums): # output = max(output, (j - i) + 1) # else: # i = find_error(int_diff, i, j, max_value, min_value, limit, nums,cache) + 1 # break # idx = j # return output
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: # print(a) # print(maxd) # print(mind) while len(maxd) and a>maxd[-1]: maxd.pop() while len(mind) and a<mind[-1]: mind.pop(); # print(maxd) # print(mind) maxd.append(a) mind.append(a) # print(maxd) # print(mind) 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
from sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: sl = SortedList() start = 0 ans = 0 for end in range(len(nums)): x = nums[end] sl.add(x) while sl[-1] - sl[0] > limit and start <= end: sl.remove(nums[start]) start += 1 if start <= end: ans = max(ans, end - start + 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 class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: min_heap = [] max_heap = [] i,j = 0,0 res = 0 while i<len(nums) and j<len(nums): heapq.heappush(min_heap,(nums[j],j)) heapq.heappush(max_heap,(-nums[j],j)) while -max_heap[0][0]-min_heap[0][0] > limit: i = min(i,min_heap[0][1],max_heap[0][1]) + 1 while max_heap[0][1]<i: heapq.heappop(max_heap) while min_heap[0][1]<i: heapq.heappop(min_heap) res = max(res,j-i+1) j += 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: maxs, mins = [], [] res = 0 start = 0 for i, n in enumerate(nums): while maxs and maxs[-1] < n: maxs.pop() while mins and mins[-1] > n: mins.pop() maxs.append(n) mins.append(n) while maxs[0] - mins[0] > limit: if maxs[0] == nums[start]: maxs.pop(0) if mins[0] == nums[start]: mins.pop(0) start += 1 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if len(nums) == 0: return 0 if len(nums) == 1: return 1 # The top of maxStack is the position of the max element currently # being used by the subarray. # As the algorithm (which grows that subarray righwards) discovers new elements that # are incompatible with the current max, we ditch this current max and check for validity # with the next max. Thus the maxStack contains a sequence of positions of the next # possible. This also means the values must be decreasing (think about why). maxStack = [] # Same for minStack, but for mins. minStack = [] result = 1 # The subarray goes from leftPos to i. leftPos = 0 for i in range(0, len(nums)): # if nums[i] exceeds the max, keep popping off the max and try # again with the next max. Naturally, we update leftPos since # the leftmost valid position is moving until we find a valid max while maxStack and nums[i] < nums[maxStack[0]] - limit: leftPos = max(leftPos, maxStack.pop(0) + 1) # We do the same for minimum while minStack and nums[i] > nums[minStack[0]] + limit: leftPos = max(leftPos, minStack.pop(0) + 1) length = i - leftPos + 1 if length > result: result = length # recall: maxStack[0] must always be the currently used maximum. And the next elements # contain the next one to try if we have to ditch maxStack[0] # This means if nums[i] is larger than # ALL the elements in maxStack, then of course nums[i] should become our current max. To do # that we pop away the entirety of maxStack and push i onto it. But if nums[i] is larger # than some elements of maxStack, we pop away only those elements and then stop. Then we push # i to the end because it represents a possible next maximum that can be used in the future while maxStack and nums[i] > nums[maxStack[-1]]: maxStack.pop() maxStack.append(i) while minStack and nums[i] < nums[minStack[-1]]: minStack.pop() minStack.append(i) 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
from sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 sl = SortedList([]) st, end, res = 0, 0, 0 for val in nums: sl.add(val) end+=1 while abs(sl[0] - sl[-1]) > limit: sl.remove(nums[st]) st+=1 res = max(res, end - st) 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
#TRY 8/19/2020 class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: max_deque = collections.deque() min_deque = collections.deque() start = 0 end = 0 max_limit_subarray = 0 for end in range(len(nums)): while min_deque and nums[end] < nums[min_deque[-1]]: min_deque.pop() min_deque.append(end) while max_deque and nums[end] > nums[max_deque[-1]]: max_deque.pop() max_deque.append(end) # max_val = nums[max_deque[0]] # min_val = nums[min_deque[0]] # limit_break = abs(max_val - min_val) while abs(nums[max_deque[0]] - nums[min_deque[0]]) > limit: #limit has been broken. # limit_break -= nums[start] start += 1 #shrink the window. if start > min_deque[0]: min_deque.popleft() if start > max_deque[0]: max_deque.popleft() #Get the best window length so far. max_limit_subarray = max(max_limit_subarray, end - start + 1) return max_limit_subarray # class Solution: # def longestSubarray(self, nums: List[int], limit: int) -> int: # # sanity check # if nums is None or len(nums) <= 1 or limit < 0: # return 1 # # sliding window approach in O(n) time # max_len, l, r = 0, 0, 0 # # notice that, the only absolute difference we need to check is the # # maximum possible absolute difference since it ensures there are no # # other absolute differences that can be bigger than @limit # max_val, min_val = 0, 0 # # dequeu method of keeping a local min and max over a sliding window in # # amortized O(1) time # # reference: https://www.nayuki.io/page/sliding-window-minimum-maximum-algorithm # min_dq, max_dq = [], [] # while r < len(nums): # val = nums[r] # # update min_dq # while len(min_dq) > 0 and val < min_dq[-1]: # min_dq.pop() # min_dq.append(val) # # update max_dq # while len(max_dq) > 0 and val > max_dq[-1]: # max_dq.pop() # max_dq.append(val) # max_val = max_dq[0] # min_val = min_dq[0] # diff = abs(max_val - min_val) # # note that we do not have to worry about the left pointer passing the right one # if diff > limit: # # recalculate min and max values of window by updating dequeues # check = nums[l] # if check == min_dq[0]: # min_dq.pop(0) # if check == max_dq[0]: # max_dq.pop(0) # # shrink window # l += 1 # # expand window # max_len = max(max_len, r - l + 1) # r += 1 # return max_len # class Solution: # def longestSubarray(self, nums: List[int], limit: int) -> int: # #int array nums # #int limit # #ret longest non-empty subarray s.t # #the absolute diff betweeen any two elemetns of the subarray is <= limit # start = 0 # end = 0 # min_ele = float('inf') # max_ele = float('-inf') # sub_array = [] # contig_sub = float('-inf') # #currently start at zero but let's see if we can try to start at the end maybe? # while end < len(nums): # sub_array = nums[start:end + 1] # limit_breaker = abs(max(sub_array) - min(sub_array)) # while limit_breaker > limit: #You are too much :'( # tmp = start # limit_breaker -= nums[tmp] # # start += 1 #This is too early because once we've subtracted we're using it # #This means that start wil lbe < 1 for contig sub potentially. # if limit_breaker <= limit: #You are okay. # contig_sub = max(contig_sub, end - start + 1) # end += 1 # return contig_sub # class Solution: # def longestSubarray(self, nums: List[int], limit: int) -> int: # #Queues to take the min and the max of our subarray so far. # min_deque, max_deque = deque(), deque() # l = r = 0 # ans = 0 # while r < len(nums): # #Min_deque has the minimum value position and check if there are better ones # while min_deque and nums[r] <= nums[min_deque[-1]]: # min_deque.pop() # min_deque.append(r) # #Max_deque has the maximum value position and check if there are better ones # while max_deque and nums[r] >= nums[max_deque[-1]]: # max_deque.pop() # max_deque.append(r) # #Limit break the limit? # #If it does then we can find the next one. # while nums[max_deque[0]] - nums[min_deque[0]] > limit: # l += 1 # #if the next position is > than our current one than we know it's no longer a part of the subarray. # 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
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 i = 0 j = 0 n = len(nums) maxq = [] minq = [] max_l = 0 while i < n and j < n: while maxq and nums[j] > maxq[-1]: maxq.pop() while minq and nums[j] < minq[-1]: minq.pop() maxq.append(nums[j]) minq.append(nums[j]) while maxq[0] - minq[0] > limit: if nums[i] == maxq[0]: maxq.pop(0) if nums[i] == minq[0]: minq.pop(0) i += 1 max_l = max(max_l, j - i + 1) j += 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: ''' 根据要求,子串任意两个数之间的差要小于等于limit 所以利用递增栈与递减栈记录子串的最大与最小值 maxs[0]-mins[0] 作为子数组的最大maxdiff ''' n=len(nums) maxs=[] #递减栈 mins=[] #递增栈 #maxs[0]-mins[0] 作为子数组的最大maxdiff l=res=0 for r in range(n): while maxs and maxs[-1]<nums[r]: maxs.pop() maxs.append(nums[r]) while mins and mins[-1]>nums[r]: mins.pop() mins.append(nums[r]) while maxs[0]-mins[0]>limit: if maxs[0]==nums[l]: maxs.pop(0) if mins[0]==nums[l]: mins.pop(0) l+=1 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: mx, mi = [0], [0] res = 1 left = 0 for c in range(1, len(nums)): num = nums[c] while mx and nums[mx[-1]] < num: mx.pop() mx.append(c) while mi and nums[mi[-1]] > num: mi.pop() mi.append(c) while mi and mx and nums[mx[0]] - nums[mi[0]] > limit: left += 1 if left > mx[0]: mx.pop(0) if left > mi[0]: mi.pop(0) res = max(res, c - 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
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 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 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: result = 0 min_deque = [] max_deque = [] start = 0 for index, value in enumerate(nums): while len(min_deque) and value < min_deque[-1]: min_deque.pop() while len(max_deque) and value > max_deque[-1]: max_deque.pop() min_deque.append(value) max_deque.append(value) while max_deque[0] - min_deque[0] > limit: if nums[start] == min_deque[0]: min_deque.pop(0) if nums[start] == max_deque[0]: max_deque.pop(0) start += 1 result = max(result, index - start + 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: from collections import deque n = len(nums) l, r = 0, -1 max_window, min_window = deque(), deque() while r<n-1: r+=1 while max_window and max_window[-1]<nums[r]: max_window.pop() while min_window and min_window[-1]>nums[r]: min_window.pop() max_window.append(nums[r]) min_window.append(nums[r]) if max_window[0]-min_window[0]>limit: if max_window[0]==nums[l]: max_window.popleft() if min_window[0]==nums[l]: min_window.popleft() l+=1 return r-l+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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: # Use sortedList to maintain the max/min of the subarray from sortedcontainers import SortedList wd = SortedList([nums[0]]) # window max_len = 1 l = 0 r = 0 while r < len(nums): minn = wd[0] maxx = wd[-1] if maxx-minn <= limit: max_len = max(max_len, r-l+1) # extend right r += 1 if r < len(nums): wd.add(nums[r]) else: # shrink left wd.discard(nums[l]) l += 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
from typing import List from sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: i, j = 0, 0 maxLen = 1 sortedInterval = SortedList([nums[0]]) while i <= j < len(nums): minVal, maxVal = sortedInterval[0], sortedInterval[-1] if maxVal - minVal <= limit: maxLen = max(j - i + 1, maxLen) j += 1 if j < len(nums): sortedInterval.add(nums[j]) elif i < j: sortedInterval.discard(nums[i]) i += 1 else: j += 1 i = j 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: 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
def add_to_max_heap(heap,value): heapq.heappush(heap,value) def add_to_min_heap(heap,value): heapq.heappush(heap,value) def get_max_val(heap): return heap[0][0] def get_min_val(heap): return heap[0][0] class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: l = 0 r = 0 ans = 0 max_heap = [] min_heap = [] while l < len(nums) and r < len(nums): # print(l,r) add_to_max_heap(max_heap,(-nums[r],r)) add_to_min_heap(min_heap,(nums[r],r)) max_val = -get_max_val(max_heap) min_val = get_min_val(min_heap) if abs(max_val-min_val) <= limit: ans = max(ans,r-l+1) r+=1 else: while min_heap and min_heap[0][1] < l+1: heapq.heappop(min_heap) while max_heap and max_heap[0][1] < l+1: heapq.heappop(max_heap) 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: min_heap = [] max_heap = [] longest_window = 1 min_idx = 0 for i, num in enumerate(nums): heapq.heappush(min_heap, (num, i)) heapq.heappush(max_heap, (-num, i)) # print(min_heap) # print(max_heap) # print(min_heap[0][0], max_heap[0][0]) while min_heap[0][1] < min_idx: heapq.heappop(min_heap) while max_heap[0][1] < min_idx: heapq.heappop(max_heap) if abs(min_heap[0][0] + max_heap[0][0]) > limit: #print('here') if (min_heap[0][1] < max_heap[0][1]): min_idx = min_heap[0][1] + 1 heapq.heappop(min_heap) #print('min_idx', min_idx) else: min_idx = max_heap[0][1] + 1 heapq.heappop(max_heap) #print(min_heap[0][1], max_heap[0][1]) #print(min_idx, i) longest_window = max(longest_window, i - min_idx + 1) return longest_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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 rlt = 1 q = collections.deque([nums[0]]) max_stack, min_stack = [nums[0]], [nums[0]] for i in range(1, len(nums)): while max_stack and (max_stack[0] - nums[i] > limit or nums[i] - min_stack[0] > limit): head = q.popleft() if head == max_stack[0]: max_stack.pop(0) if head == min_stack[0]: min_stack.pop(0) q.append(nums[i]) while max_stack and max_stack[-1] < nums[i]: max_stack.pop() while min_stack and min_stack[-1] > nums[i]: min_stack.pop() max_stack.append(nums[i]) min_stack.append(nums[i]) rlt = max(rlt, len(q)) return rlt
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
# [10, 1, 2, 4, 7, 2] # pqmax: [(-7, 4), (-4, 3), (-2, 2), (-1, 1), (-2, 5)] # pqmin: [(2, 2), (2, 5), (7, 4), (10, 0), (4, 3)] # end: 5 # start: 2 import heapq class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not len(nums): return 0 output = 1 s_idx = 0 e_idx = 0 pqmin = [] pqmax = [] for e_idx in range(len(nums)): heapq.heappush(pqmin, (nums[e_idx], e_idx)) heapq.heappush(pqmax, (-nums[e_idx], e_idx)) while s_idx < e_idx and abs(pqmax[0][0] + pqmin[0][0]) > limit: s_idx += 1 while pqmax and pqmax[0][1] < s_idx: heapq.heappop(pqmax) while pqmin and pqmin[0][1] < s_idx: heapq.heappop(pqmin) output = max(output, e_idx-s_idx+1) return output
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) while 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 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 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: winMax = [0] winMin = [0] i = j = 0 res = 0 while i<len(nums)-1 and j<len(nums)-1: diff = nums[winMax[0]] - nums[winMin[0]] #print(i,j,diff,winMax,winMin) if diff <= limit: res = max(res,j-i+1) if diff <= limit: j += 1 while winMax and nums[j]>nums[winMax[-1]]: winMax.pop(-1) winMax.append(j) while winMin and nums[j]<nums[winMin[-1]]: winMin.pop(-1) winMin.append(j) else: if i == winMax[0]: winMax.pop(0) if i == winMin[0]: winMin.pop(0) i += 1 diff = nums[winMax[0]] - nums[winMin[0]] if diff <= limit: 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 bisect class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if limit < 0: return 0 n = len(nums) if n < 2: return n # sliding window to calculate min-max range # extend window if range is within limit r --> # shrink window if range is off limit l --> # sliding window is a sorted subarray longest = 1 l, r = 0, 0 window = [] while l < n and r < n: if not window: window.append(nums[r]) r += 1 else: curr = nums[r] minimum, maximum = window[0], window[-1] if abs(curr - minimum) <= limit and abs(curr - maximum) <= limit: bisect.insort(window, curr) r += 1 longest = max(longest, r - l) else: window.remove(nums[l]) 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: max_q, min_q = [],[] ans = 1 l = 0 for i,num in enumerate(nums): while len(max_q) and nums[max_q[-1]] < num: max_q.pop(-1) while len(min_q) and nums[min_q[-1]] > num: min_q.pop(-1) min_q.append(i) max_q.append(i) # print(max_q, min_q) # max_num = max_q[0] # min_num = min_q[0] while(nums[max_q[0]]-nums[min_q[0]] > limit): if max_q[0] > min_q[0]: l = min_q.pop(0) + 1 else: l = max_q.pop(0) + 1 # ans = max(abs(max_q[0]-min_q[0]) +1, ans) ans = max(i-l +1, 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: incr_q = [nums[0]] decr_q = [nums[0]] res = 1 left = 0 right = 1 while right < len(nums): while incr_q and incr_q[-1] > nums[right]: incr_q.pop() incr_q.append(nums[right]) while decr_q and decr_q[-1] < nums[right]: decr_q.pop() decr_q.append(nums[right]) while incr_q and decr_q and decr_q[0] - incr_q[0] > limit: if nums[left] == incr_q[0]: incr_q.pop(0) if nums[left] == decr_q[0]: decr_q.pop(0) left += 1 res = max(right-left+1, res) 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
from heapq import heappush, heappop class Solution: def longestSubarray(self, nums, limit): discard = -1 res = 0 minq, maxq = [], [] for i in range(len(nums)): while (minq and abs(nums[i] - minq[0][0]) > limit): _, index = heappop(minq) discard = max(discard, index) heappush(minq, (nums[i], i)) while (maxq and abs(nums[i] + maxq[0][0]) > limit): _, index = heappop(maxq) discard = max(discard, index) heappush(maxq, (-nums[i], i)) res = max(res, i - discard) 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 len(nums) == 0: return 0 if len(nums) == 1: return 1 #left and right index to take out the substring left = 0 right = 1 #let current max and min equal to the first number cur_max = cur_min = nums[0] #the max lense of the target substring is at least 1 max_len = 1 while right < len(nums): cur_max = max(cur_max, nums[right]) cur_min = min(cur_min, nums[right]) if cur_max - cur_min <= limit: max_len = max(max_len, right - left + 1) else: if nums[left] == cur_max: cur_max = max(nums[left + 1:right + 1]) if nums[left] == cur_min: cur_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 right = 1 ans = 1 currMax = nums[0] currMin = nums[0] while left <= right and right < len(nums): currMax = max(currMax, nums[right]) currMin = min(currMin, nums[right]) if currMax - currMin <= limit: ans = max(ans, right - left + 1) else: if nums[left] == currMax: currMax = max(nums[left+1: right+1]) if nums[left] == currMin: currMin = min(nums[left+1: right+1]) left += 1 right += 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 sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: s = SortedList() j = ans = 0 for i, val in enumerate(nums): s.add(val) while s and s[-1] - s[0] > limit: s.remove(nums[j]) j += 1 ans = max(ans, (i - j + 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 bisect import insort class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: win = [] n = len(nums) i = j = 0 max_len = 0 while j < n: if not win: win.append(nums[j]) else: insort(win, nums[j]) while win[-1] - win[0] > limit: win.remove(nums[i]) i += 1 if j - i + 1 > max_len: max_len = j - i + 1 j += 1 return max_len # time: O(n^2) # space: O(n)
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 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 len(nums) == 0: return 0 dec_stack = [] # stores all the next largest value (value, pos) inc_stack = [] # stores all the next min value (value, pos) max_interval = 1 # two pointer expand and contract, including l, r = 0, 0 min_v, max_v = nums[0], nums[0] dec_stack.append((nums[0], 0)) inc_stack.append((nums[0], 0)) while l < len(nums) and r < len(nums): while max_v - min_v <= limit: max_interval = max(max_interval, r - l + 1) r += 1 if r == len(nums): break # added new values update our stacks while dec_stack and nums[r] > dec_stack[-1][0]: dec_stack.pop(-1) dec_stack.append((nums[r], r)) max_v = dec_stack[0][0] while inc_stack and nums[r] < inc_stack[-1][0]: inc_stack.pop(-1) inc_stack.append((nums[r], r)) min_v = inc_stack[0][0] # now the interval does not fit the limit, contract while max_v - min_v > limit: if dec_stack[0][1] == l: dec_stack.pop(0) max_v = dec_stack[0][0] if inc_stack[0][1] == l: inc_stack.pop(0) min_v = inc_stack[0][0] l += 1 return max_interval
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 sortedcontainers import SortedList sl = SortedList() l = r = 0 result = 1 for r in range(len(nums)): sl.add(nums[r]) while sl[-1] - sl[0] > limit: sl.remove(nums[l]) l += 1 result = max(len(sl), result) 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: if not nums: return 0 curr_max = nums[0] # 当子数组下最大值 这里初始化为第一个数 curr_min = nums[0] # 当子数组下最大值 这里初始化为第一个数 sub_nums = [] # 以数组作为窗口滑动 for num in nums: if abs(num - curr_max) <= limit and abs(num - curr_min) <= limit and abs(curr_max - curr_min) <= limit: curr_max = max(num,curr_max) curr_min = min(num,curr_min) sub_nums.append(num) else: #缩小窗口 sub_nums.append(num)#先append 当前数 sub_nums.pop(0)#移除最左边 curr_max = max(sub_nums) # 更新最值 curr_min = min(sub_nums) return len(sub_nums)
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 curr_max = nums[0] # 当子数组下最大值 这里初始化为第一个数 curr_min = nums[0] # 当子数组下最大值 这里初始化为第一个数 sub_nums = [] # 以数组作为窗口滑动 for num in nums: if abs(num - curr_max) <= limit and abs(num - curr_min) <= limit and abs(curr_max - curr_min) <= limit: curr_max = max(num,curr_max) curr_min = min(num,curr_min) sub_nums.append(num) else: #缩小窗口 sub_nums.append(num) sub_nums.pop(0) curr_max = max(sub_nums) # 当子数组最大值 curr_min = min(sub_nums) # 当前子数组最小值 return len(sub_nums)
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: low = None curr_max = None curr_low = -1 max_len = 0 min_queue = [] max_queue = [] for i in range (len(nums)): curr_num = nums[i] self.queue_add_max(max_queue,curr_num,i) self.queue_add_min(min_queue,curr_num,i) curr_max = max_queue[0][0] curr_min = min_queue[0][0] if (curr_max - curr_min) > limit: while ((curr_max - curr_min) > limit) and (curr_low < i): curr_low += 1 if (curr_low == min_queue[0][1]): min_queue.pop(0) if (curr_low == max_queue[0][1]): max_queue.pop(0) curr_max = max_queue[0][0] curr_min = min_queue[0][0] curr_dist = i - curr_low if (curr_dist > max_len): max_len = curr_dist return max_len def queue_add_max(self,queue,element,index): while (queue != []): if element > queue[len(queue) - 1][0]: queue.pop() else: break queue.append((element,index)) def queue_add_min(self,queue,element,index): while (queue != []): if element < queue[len(queue) - 1][0]: queue.pop() else: break queue.append((element,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: if not nums: return 0 cur_max = nums[0] cur_min = nums[0] sub_nums = [] # 以数组作为窗口滑动 for i in nums: cur_max = max(i,cur_max) cur_min = min(i,cur_min) if cur_max - cur_min <= limit: sub_nums.append(i) else: sub_nums.append(i) sub_nums.pop(0) cur_max = max(sub_nums) cur_min = min(sub_nums) return len(sub_nums)
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: maxd = collections.deque() mind = collections.deque() i = 0 N = len(nums) Lm = 0 for j in range(N): a = nums[j] while maxd and maxd[-1]<a: maxd.pop() maxd.append(a) while mind and mind[-1]>a: mind.pop() mind.append(a) while maxd[0]-mind[0]>limit: if maxd[0] == nums[i]: maxd.popleft() if mind[0] == nums[i]: mind.popleft() i += 1 Lm = max(Lm, j-i+1) return Lm
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: # print('start') import sortedcontainers sl = sortedcontainers.SortedList() start, end = 0, 0 best = 0 while end < len(nums): sl.add(nums[end]) if sl[-1] - sl[0] <= limit: best = max(best, len(sl)) else: while sl[-1] - sl[0] > limit: sl.remove(nums[start]) start += 1 end += 1 # print(len(sl)) 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: if not nums: return 0 cur_max = nums[0] cur_min = nums[0] sub_nums = collections.deque() for i in nums: cur_max = max(i,cur_max) cur_min = min(i,cur_min) if cur_max - cur_min <= limit: sub_nums.append(i) else: sub_nums.append(i) sub_nums.popleft() cur_max = max(sub_nums) cur_min = min(sub_nums) return len(sub_nums)
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: #https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609701/Python-two-heap-solution 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) elif 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
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: # 2:37 maxD = [] minD = [] ind = 0 res = 0 slow = 0 fast = 0 while slow <= fast and fast < len(nums): while len(maxD) > 0 and maxD[len(maxD)-1] < nums[fast]: maxD.pop(len(maxD)-1) maxD.append(nums[fast]) while len(minD) > 0 and minD[len(minD)-1] > nums[fast]: minD.pop(len(minD)-1) minD.append(nums[fast]) while maxD[0] - minD[0] > limit and slow < fast: if maxD[0] == nums[slow]: maxD.pop(0) if minD[0] == nums[slow]: minD.pop(0) slow += 1 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, nums: List[int], limit: int) -> int: if nums is None or len(nums)==0: return 0; queue=collections.deque([]) max_len=0 right=0 min_heap=[] max_heap=[] for left in range(len(nums)): if left==right: heapq.heappush(min_heap, (nums[left], left)) heapq.heappush(max_heap,(-nums[left], left)) right+=1 while right<len(nums): cur_min=min_heap[0][0] cur_max=-max_heap[0][0] if (abs(nums[right]-cur_min)>limit): break if (abs(nums[right]-cur_max)>limit): break heapq.heappush(min_heap,(nums[right], right)) heapq.heappush(max_heap,(-nums[right], right)) right+=1 max_len=max(max_len, right-left) if right==len(nums): return max_len while max_heap and max_heap[0][1]<=left: heapq.heappop(max_heap) while min_heap and min_heap[0][1]<=left: heapq.heappop(min_heap) 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]) maxindex=0 while i<=j and j<=len(nums)-1: 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) maxindex=max(maxindex,index) while len(maxheap)>0 and abs(-maxheap[0][0]-nums[j])>limit: ele,index=heapq.heappop(maxheap) maxindex=max(maxindex,index) 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
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: arr = nums lim = limit p1 = 0 p2 = 0 ans = 1 bt = SortedList([arr[0]]) n = len(arr) while p1 < n-1 or p2 < n-1: mx = bt[-1] mn = bt[0] diff = abs(mx-mn) if diff <= lim and ans < p2 - p1 + 1: ans = p2-p1 + 1 if p2 == n-1 or diff > lim: bt.remove(arr[p1]) p1 += 1 else: p2 += 1 bt.add(arr[p2]) 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): maxQ, minQ = deque(), deque() i = 0 for val in 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 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 res 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 > nums[maxQ[-1]]: maxQ.pop() while minQ and val < nums[minQ[-1]]: minQ.pop() maxQ.append(j) minQ.append(j) if nums[maxQ[0]] - nums[minQ[0]] > limit: if maxQ[0] == i: maxQ.popleft() if minQ[0] == i: minQ.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: max_stack = [] min_stack = [] left = 0 ans = 0 for right in range(len(nums)): while max_stack and nums[max_stack[-1]] < nums[right]: max_stack.pop() max_stack.append(right) while min_stack and nums[min_stack[-1]] > nums[right]: min_stack.pop() min_stack.append(right) while max_stack and min_stack and nums[max_stack[0]] - nums[min_stack[0]] > limit: if max_stack[0] <= left: max_stack.pop(0) if min_stack[0] <= left: min_stack.pop(0) left += 1 ans = max(ans, right - left + 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 heapq import heappush, heappop 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 min_q = [] max_q = [] start_index = 0 cur_longest_len = 0 for i, number in enumerate(nums): heappush(min_q, (number, i)) heappush(max_q, (-number, i)) while self.calc_diff(-max_q[0][0], min_q[0][0]) > limit: # print(self.calc_diff(-max_q[0][0], min_q[0][0]) ) if min_q[0][1] > max_q[0][1]: value, _index = heappop(max_q) q = max_q else: value, _index = heappop(min_q) q = min_q start_index = max(_index + 1, start_index) # print(start_index, value, q) # print(start_index, _index, value) while q and q[0][1] < start_index: heappop(q) cur_longest_len = max( cur_longest_len, i - start_index + 1 ) # if cur_longest_len == 3: # print(\"HERE\", start_index, i) # print(max_q) # print(min_q) return cur_longest_len 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
import bisect class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if len(nums) <= 1: return len(nums) h = [nums[0]] right, left, max_len = 0, 0, 1 lo, hi = nums[0], nums[0] while right < len(nums) and left < len(nums): if abs(hi-lo) <= limit: max_len = max(max_len, right-left+1) right += 1 if right < len(nums): hi = max(nums[right], hi) lo = min(nums[right], lo) bisect.insort(h, nums[right]) else: del h[bisect.bisect_left(h, nums[left])] lo, hi = h[0], h[-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: maxheap, minheap = [(-nums[0], 0)], [(nums[0], 0)] l, res = 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 - l + 1) else: while minheap and val - minheap[0][0] > limit: v, index = heapq.heappop(minheap) l = max(l, index + 1) while maxheap and -maxheap[0][0] - val > limit: v, index = heapq.heappop(maxheap) l = max(l, 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
import heapq class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: n = len(nums) left = 0 right = 0 max_d = 0 min_heap = [nums[0]] max_heap = [-nums[0]] local_max = nums[0] local_min = nums[0] while left < n and right < n: if nums[right] > local_max: local_max = nums[right] if nums[right] < local_min: local_min = nums[right] lower = local_max - limit upper = local_min + limit if lower <= nums[right] <= upper: if right - left + 1 > max_d: max_d = right - left + 1 right += 1 if right < n: heapq.heappush(min_heap, nums[right]) heapq.heappush(max_heap, -nums[right]) else: if nums[left] == min_heap[0]: heapq.heappop(min_heap) else: min_heap.remove(nums[left]) if max_heap[0] == -nums[left]: heapq.heappop(max_heap) else: max_heap.remove(-nums[left]) left += 1 local_min = min_heap[0] local_max = -max_heap[0] return max_d # def longestSubarray(self, nums: List[int], limit: int) -> int:sub: # n = len(nums) # max_d = 0 # for left in range(n): # local_max = float('-inf') # local_min = float('inf') # for right in range(left + 1, n + 1): # sub = nums[left:right] # if local_max < sub[-1]: # local_max = sub[-1] # if local_min > sub[-1]: # local_min = sub[-1] # if local_max - local_min <= limit: # if right - left + 1 > max_d: # max_d = right - left + 1 # # print(max_d, right, left, sub) # else: # break # return max_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: def addToDeqs(min_deq, max_deq, curr, start_i): while min_deq and min_deq[-1] > curr: min_deq.pop() min_deq.append(curr) while max_deq and max_deq[-1] < curr: max_deq.pop() max_deq.append(curr) while abs(max_deq[0] - min_deq[0]) > limit: if max_deq[0] == nums[start_i]: max_deq.pop(0) if min_deq[0] == nums[start_i]: min_deq.pop(0) start_i += 1 return start_i min_deq = [] max_deq = [] longest_subarray = 0 start_i = 0 for i, n in enumerate(nums): start_i = addToDeqs(min_deq, max_deq, n, start_i) length_subarray = (i-start_i)+1 longest_subarray = max(longest_subarray, length_subarray) return longest_subarray
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: ans=0 heap1=[] heap2=[] end=0 start=0 n=len(nums) while end<n: heapq.heappush(heap1,[-nums[end],end]) heapq.heappush(heap2,[nums[end],end]) while (-heap1[0][0])-heap2[0][0]>limit: start+=1 while heap1 and heap1[0][1]<start: heapq.heappop(heap1) while heap2 and heap2[0][1]<start: heapq.heappop(heap2) ans=max(ans,end-start+1) end+=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: n = len(nums) res = 0 left, right = 0, 0 minimum, maximum = [], [] while right < n: heapq.heappush(minimum, (nums[right], right)) heapq.heappush(maximum, (-nums[right], right)) right += 1 while -maximum[0][0] - minimum[0][0] > limit: # print(\"pop\", left, right, res, minimum, maximum, ) while minimum and minimum[0][1] <= left: heapq.heappop(minimum) while maximum and maximum[0][1] <= left: heapq.heappop(maximum) left += 1 res = max(res, right - left) # print(left, right, res, minimum, maximum, ) 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 = 1 maxi = nums[0] mini = nums[0] start = 0 temp = nums[start:start+1] for i in range(1, len(nums)): temp.append(nums[i]) if nums[i] > maxi: maxi = nums[i] if nums[i] < mini: mini = nums[i] if maxi - mini <= limit: max_len = max(max_len, len(temp)) else: temp.pop(start) maxi = max(temp) mini = min(temp) 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 = [], [] result = i = 0 for j in range(len(nums)): heapq.heappush(minHeap, [nums[j], j]) heapq.heappush(maxHeap, [-nums[j], j]) while -maxHeap[0][0] - minHeap[0][0] > limit: i = min(maxHeap[0][1], minHeap[0][1]) + 1 while minHeap[0][1] < i: heapq.heappop(minHeap) while maxHeap[0][1] < i: heapq.heappop(maxHeap) result = max(result, j - i + 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: max_len = 0 maxi = nums[0] mini = nums[0] start = 0 temp = [] for i in range(len(nums)): temp.append(nums[i]) if nums[i] > maxi: maxi = nums[i] if nums[i] < mini: mini = nums[i] if maxi - mini <= limit: max_len = max(max_len, len(temp)) else: temp.pop(start) maxi = max(temp) mini = min(temp) 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: max_stack = [] min_stack = [] l = 0 ans = 0 for r in range(len(nums)): while max_stack and nums[max_stack[-1]] < nums[r]: max_stack.pop() max_stack.append(r) while min_stack and nums[min_stack[-1]] > nums[r]: min_stack.pop() min_stack.append(r) while max_stack and min_stack and nums[max_stack[0]] - nums[min_stack[0]] > limit: if max_stack[0] <= l: max_stack.pop(0) if min_stack[0] <= l: min_stack.pop(0) l += 1 ans = max(ans, r - 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: maxd = collections.deque() mind = collections.deque() s, e = 0, 0 res = 0 while e < len(nums): while len(maxd) and nums[e] >= nums[maxd[-1]]: maxd.pop() while len(mind) and nums[e] <= nums[mind[-1]]: mind.pop() maxd.append(e) mind.append(e) if nums[maxd[0]] - nums[mind[0]] > limit: s += 1 if s > mind[0]: mind.popleft() if s > maxd[0]: maxd.popleft() else: res = max(res, e - s + 1) e += 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 sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: lhs = 0 rhs = 1 diff_len = 1 sd = SortedList([nums[lhs]]) # map value -> count while rhs < len(nums): # print(f\"lhs: {lhs}, rhs: {rhs}\") sd.add(nums[rhs]) # print(sd) if (sd[len(sd)-1] - sd[0] <= limit): # print(f\"{sd[len(sd)-1]}-{sd[0]} > {limit}\") diff_len = max(diff_len, rhs-lhs+1) # print(f\"diff_len {diff_len}\") else: sd.discard(nums[lhs]) lhs += 1 rhs += 1 return diff_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 MinMaxList(object): def __init__(self): self._container = {} self._max = -6665677 self._min = 6665677 def min_val(self) -> int: return self._min def max_val(self) -> int: return self._max def append_val(self, val: int): if val in self._container: self._container[val] = self._container[val] + 1; else: self._container[val] = 1 self._max = max(val, self._max) self._min = min(val, self._min) def remove_val(self, val: int): if val in self._container: if self._container[val] > 1: self._container[val] = self._container[val] - 1; return else: del self._container[val] if self._max == val: self._max = max(self._container.keys()) if self._min == val: self._min = min(self._container.keys()) class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: diff = 0 max_result = 0 max_subarray = 0 low=0 high=0 window = MinMaxList() while high < len(nums): window.append_val(nums[high]) high = high+1 max_subarray = max_subarray + 1 diff = abs(window.max_val() - window.min_val()) while diff > limit: window.remove_val(nums[low]) low = low+1 max_subarray = max_subarray -1 diff = abs(window.max_val() - window.min_val()) max_result = max(max_subarray, max_result) return max_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: l = len(nums) if l == 1: if limit >= 0: return 1 return 0 max_count = i =0 j = 1 def get_min(): while min_list[0][1] < i: heappop(min_list) return min_list[0][1] def get_max(): while max_list[0][1] < i: heappop(max_list) return max_list[0][1] min_list = [] max_list = [] heappush(min_list, (nums[0], 0)) heappush(max_list, (-nums[0], 0)) while l > j >= i: heappush(min_list, (nums[j], j)) heappush(max_list, (-nums[j], j)) mn = get_min() mx = get_max() diff = nums[mx]-nums[mn] if diff > limit: if j != i+1: max_count = max(max_count,j-i) i = min(mn, mx) + 1 else: j += 1 i += 1 else: j+=1 if j == l and i < j-1: max_count = max(max_count,j-i) return max_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: curr_max = deque([]) curr_min = deque([]) i = 0 j = 0 res = 0 while i<=j and j < len(nums): num = nums[j] while curr_max and curr_max[-1] < num: curr_max.pop() else: curr_max.append(num) while curr_min and curr_min[-1] > num: curr_min.pop() else: curr_min.append(num) if curr_max[0] - curr_min[0] <= limit: res = max(res, j - i + 1) else: value = nums[i] if value == curr_max[0]: curr_max.popleft() if value == curr_min[0]: curr_min.popleft() i += 1 j += 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 = [] left = 0 res = 0 for i in range(len(nums)): heapq.heappush(maxq, [-nums[i], i]) heapq.heappush(minq, [nums[i], i]) 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) # print(maxq) # print(minq) 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: min_deque = [nums[0]] max_deque = [nums[0]] max_stretch = 1 start = 0 end = 0 while end < len(nums) - 1: end += 1 while len(min_deque) > 0 and nums[end] < min_deque[-1]: min_deque.pop() min_deque.append(nums[end]) while len(max_deque) > 0 and nums[end] > max_deque[-1]: max_deque.pop() max_deque.append(nums[end]) while max_deque[0] - min_deque[0] > limit: if min_deque[0] == nums[start]: min_deque.pop(0) if max_deque[0] == nums[start]: max_deque.pop(0) start += 1 if end - start + 1 > max_stretch: max_stretch = end - start + 1 return max_stretch # min_heap = [nums[0]] # max_heap = [-nums[0]] # min_removed = {} # max_removed = {} # max_stretch = 1 # start = 0 # end = 0 # while end < len(nums) - 1: # end += 1 # heapq.heappush(min_heap, nums[end]) # heapq.heappush(max_heap, -nums[end]) # while - max_heap[0] - min_heap[0] > limit: # if nums[start] not in min_removed: # min_removed[nums[start]] = 0 # min_removed[nums[start]] += 1 # if nums[start] not in max_removed: # max_removed[nums[start]] = 0 # max_removed[nums[start]] += 1 # while -max_heap[0] in max_removed and max_removed[-max_heap[0]] > 0: # max_removed[-max_heap[0]] -= 1 # heapq.heappop(max_heap) # while min_heap[0] in min_removed and min_removed[min_heap[0]] > 0: # min_removed[min_heap[0]] -= 1 # heapq.heappop(min_heap) # start += 1 # if end - start + 1 > max_stretch: # max_stretch = end - start + 1 # return max_stretch
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 max_val = nums[0] min_val = nums[0] counts = {nums[0] : 1} subarray = deque([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: max_val = max(subarray) break elif min_val == num: min_val = min(subarray) 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
from sortedcontainers import SortedDict class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: startWindow = 0 runningDict = SortedDict() maxWindow = 0 for endWindow in range(len(nums)): if nums[endWindow] not in runningDict: runningDict[nums[endWindow]] = 0 runningDict[nums[endWindow]] += 1 minKey, maxKey = runningDict.peekitem(0), runningDict.peekitem(-1) minKey, maxKey = minKey[0], maxKey[0] while abs(maxKey - minKey) > limit and startWindow < endWindow: runningDict[nums[startWindow]] -= 1 if runningDict[nums[startWindow]] == 0: del runningDict[nums[startWindow]] minKey, maxKey = runningDict.peekitem(0), runningDict.peekitem(-1) minKey, maxKey = minKey[0], maxKey[0] startWindow += 1 maxWindow = max(maxWindow, endWindow - startWindow + 1) return maxWindow
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 Item: val, index= 0, 0 def __lt__(self, other): return self.val < other.val def __init__(self, val, index): self.val = val self.index = index class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: maxheap, minheap = [], [] index = 0 maxLen = 0 leftIndex = -1 while index < len (nums): while len(minheap)> 0 and (minheap[0].val< nums[index] - limit or minheap[0].index < leftIndex): leftIndex = max (leftIndex, heapq.heappop(minheap).index) while len(maxheap) > 0 and (-maxheap[0].val > nums[index] + limit or maxheap[0].index < leftIndex ): leftIndex = max (leftIndex, heapq.heappop(maxheap).index) heapq.heappush(minheap, Item(nums[index], index)) heapq.heappush(maxheap, Item(-nums[index], index)) maxLen = max(maxLen, index - leftIndex) index += 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: l = ans = 0 maxh, minh = [], [] for r, num in enumerate(nums): heapq.heappush(maxh, (-1 * num, r)) heapq.heappush(minh, (num, r)) while maxh[0][0] * -1 - minh[0][0] > limit: while maxh[0][1] <= l: heapq.heappop(maxh) while minh[0][1] <= l: heapq.heappop(minh) l += 1 ans = max(ans, r - 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
from sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: # lhs = 0 # rhs = 1 # diff_len = 1 # sd = SortedList([nums[lhs]]) # map value -> count # while rhs < len(nums): # # print(f\"lhs: {lhs}, rhs: {rhs}\") # sd.add(nums[rhs]) # # print(sd) # if (sd[len(sd)-1] - sd[0] <= limit): # # print(f\"{sd[len(sd)-1]}-{sd[0]} > {limit}\") # diff_len = max(diff_len, rhs-lhs+1) # # print(f\"diff_len {diff_len}\") # else: # sd.discard(nums[lhs]) # lhs += 1 # rhs += 1 # return diff_len 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: maxque = collections.deque([]) minque = collections.deque([]) start = end = 0 count = 0 while end < len(nums): while len(maxque) and nums[end] > nums[maxque[-1]]: maxque.pop() while len(minque) and nums[end] < nums[minque[-1]]: minque.pop() maxque.append(end) minque.append(end) if nums[maxque[0]] - nums[minque[0]] > limit: if maxque[0] == start: maxque.popleft() if minque[0] == start: minque.popleft() start += 1 end += 1 else: end += 1 return len(nums) - start
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 minQueue(object): def __init__(self): self.queue = collections.deque() # append val in constant time def append(self, val, idx): while self.queue and self.queue[-1][0] > val: self.queue.pop() self.queue.append((val, idx)) #print(\"minQueue is\", self.queue) def getMin(self, left_idx): while self.queue and self.queue[0][1] < left_idx: self.queue.popleft() #print(\"l is\", left_idx, \"minQ, \",self.queue) return self.queue[0][0] class maxQueue(object): def __init__(self): self.queue = collections.deque() # append val in constant time def append(self, val, idx): while self.queue and self.queue[-1][0] < val: self.queue.pop() self.queue.append((val, idx)) #print(\"maxqueue is\", self.queue) def getMax(self, left_idx): while self.queue and self.queue[0][1] < left_idx: self.queue.popleft() #print(\"l is\", left_idx, \"minQ, \" ,self.queue) return self.queue[0][0] class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: # condition is getMax - getMin <= limit minQ = minQueue() maxQ = maxQueue() l, r =0, 0 minQ.append(nums[0], 0) maxQ.append(nums[0], 0) cond = True n = len(nums) res = 0 while l <= r and r < n: # window meaning: l and r is included in the window while r<n and cond: #print(\"cond true l, r is\", l, r) res = max(res, r-l+1) r += 1 if r < n: maxQ.append(nums[r], r) minQ.append(nums[r], r) cond = maxQ.getMax(l) - minQ.getMin(l) <= limit while l<=r and not cond: #print(\"cond false l, r is\", l, r) l += 1 #print(maxQ.getMax(l), minQ.getMin(l)) cond = maxQ.getMax(l) - minQ.getMin(l) <= limit 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 l=0 r=1 curmax= nums[0] curmin=nums[0] maxlen=1 while l<=r and 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]==curmax: curmax= max(nums[l+1:r+1]) if nums[l]==curmin: curmin=min(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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: l = 0 thing = 0 curMin = 9999999 curMax = -9999999 numMap = {} i = 0 while i < len(nums): if nums[i] not in numMap: numMap[nums[i]] = 0 numMap[nums[i]] += 1 curMax = max(curMax, nums[i]) curMin = min(curMin, nums[i]) i += 1 if curMax - curMin <= limit: thing = max(thing, i - l) else: p = nums[l] numMap[nums[l]] -= 1 if numMap[nums[l]] == 0: del numMap[nums[l]] l = l + 1 if p == curMin or p == curMax: curMin = min(numMap) curMax = max(numMap) return thing
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: count = collections.defaultdict(int) min_heap = [] max_heap = [] ans = [] max_length = 0 for num in nums: ans.append(num) count[num] += 1 if count[num] == 1: heapq.heappush(min_heap, num) heapq.heappush(max_heap, num * -1) max_val = abs(max_heap[0]) min_val = min_heap[0] while max_val - min_val > limit: popped_val = ans.pop(0) count[popped_val] -= 1 # print(count) # print(min_heap) # print(ans) # print(min_val) # print(max_val) # Pop min_heap until you have a valid min_val if popped_val == min_val and count[popped_val] == 0: while count[min_val] == 0: heapq.heappop(min_heap) min_val = min_heap[0] # Pop max_heap until you have a valid max_val if popped_val == max_val and count[popped_val] == 0: while count[max_val] == 0: heapq.heappop(max_heap) max_val = abs(max_heap[0]) max_length = max(max_length, len(ans)) 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: start = 0 result = 0 minHeap = [] maxHeap = [] for i in range(len(nums)): heapq.heappush(minHeap, [nums[i], i]) heapq.heappush(maxHeap, [-nums[i], i]) while -maxHeap[0][0] - minHeap[0][0] > limit: start = min(maxHeap[0][1], minHeap[0][1]) + 1 while minHeap and minHeap[0][1] < start: heapq.heappop(minHeap) while maxHeap and maxHeap[0][1] < start: heapq.heappop(maxHeap) result = max(result, i - start + 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: if len(nums) == 0: return 0 max_q = MonoQueue(False) min_q = MonoQueue(True) l, r = 0, 0 max_q.push(nums[0]) min_q.push(nums[0]) def in_limit(): if max_q.is_empty() is True: return True return abs(max_q.top() - min_q.top()) <= limit res = 0 while r < len(nums): # print(max_q.queue, min_q.queue) if in_limit() is True: res = max(res, r-l+1) r += 1 if r < len(nums): min_q.push(nums[r]) max_q.push(nums[r]) else: min_q.popfront(nums[l]) max_q.popfront(nums[l]) l += 1 return res class MonoQueue(): def __init__(self, inc=True): self.queue = [] self.inc = inc def popfront(self, n): if self.queue[0] == n: self.queue.pop(0) return def push(self, n): while self.queue: if self.inc is True and self.queue[-1] > n: self.queue.pop(-1) elif self.inc is False and self.queue[-1] < n: self.queue.pop(-1) else: break self.queue.append(n) return def is_empty(self): return len(self.queue) == 0 def top(self): return self.queue[0]
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 __init__(self): self.nums = None self.start = None self.end = None self.q = [] self.qmx = [] def initialize (self): self.start = 0 self.end=0 heapq.heappush(self.q,[self.nums[0],0]) heapq.heappush(self.qmx,[self.nums[0]*-1,0]) def moveStartRight(self): #[2,5,3,1] 3 # 1,2,3,5 # 5,3,2,1 count = 0 # get min and max #move to the right until the min or max are popped up minValuePos = self.q[0][1] maxValuePos = self.qmx[0][1] self.start = min (minValuePos,maxValuePos) +1 #update new q while self.q[0][1] < self.start: heapq.heappop(self.q) while self.qmx[0][1] < self.start: heapq.heappop(self.qmx) #self.q = [] #for i in range(self.start, self.end +1): # self.q.append([self.nums[i],i]) #heapq.heapify(self.q) def moveEndRight(self): self.end +=1 if self.end < len(self.nums): heapq.heappush(self.q,[self.nums[self.end], self.end]) heapq.heappush(self.qmx,[self.nums[self.end]*-1, self.end]) def getMinMax(self): return self.q[0][0], self.qmx[0][0]*-1 def longestSubarray(self, nums: List[int], limit: int) -> int: self.nums = nums self.initialize() size = 0 while (self.end < len(self.nums)): minValue,maxValue = self.getMinMax() #print(self.start, self.end, minValue,maxValue) if maxValue - minValue <= limit: #print(minValue, maxValue, nums[start:end+1]) size = max(size, self.end - self.start + 1) #extend self.moveEndRight() else: self.moveStartRight() return 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
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) while maxQ[0] - minQ[0] > limit: if maxQ[0] == nums[i]: maxQ.popleft() elif minQ[0] == nums[i]: minQ.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: 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: l = len(nums) if l == 1: if limit >= 0: return 1 return 0 max_count = i =0 j = 1 def get_min(): while min_list[0][1] < i: heappop(min_list) return min_list[0][1] def get_max(): while max_list[0][1] < i: heappop(max_list) return max_list[0][1] min_list = [] max_list = [] heappush(min_list, (nums[0], 0)) heappush(max_list, (-nums[0], 0)) while l > j > i: heappush(min_list, (nums[j], j)) heappush(max_list, (-nums[j], j)) mn = get_min() mx = get_max() diff = nums[mx]-nums[mn] if diff > limit: if j != i+1: max_count = max(max_count,j-i) i += 1 else: j += 1 i += 1 else: j+=1 if j == l and i < j-1: max_count = max(max_count,j-i) return max_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
from sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 pq_min = SortedList([]) pq_max = SortedList([]) st, end, res = 0, 0, 0 for val in nums: pq_min.add(val) pq_max.add(-val) end+=1 #print(pq_min) #print(pq_max) #print(\" - \") while abs(pq_min[0] + pq_max[0]) > limit: pq_min.remove(nums[st]) pq_max.remove(-nums[st]) st+=1 #print(pq_min) #print(pq_max) #print(\" ----------- \") res = max(res, end - st) 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 l, r = 0, 1 cur_mx, cur_mi = nums[0], nums[0] max_l = 1 while l <= r and r < len(nums): cur_mx = max(cur_mx, nums[r]) cur_mi = min(cur_mi, nums[r]) if cur_mx - cur_mi <= limit: max_l = max(max_l, r - l + 1) r += 1 else: cur_mx = max(nums[l+1: r+1]) cur_mi = min(nums[l+1: r+1]) r += 1 l += 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
import heapq class Item: val, index= 0, 0 def __lt__(self, other): return self.val < other.val def __init__(self, val, index): self.val = val self.index = index class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: maxheap, minheap = [], [] index = 0 maxLen = 0 leftIndex = -1 while index < len (nums): if (len(minheap)> 0 and minheap[0].val < nums[index] - limit): while len(minheap)> 0 and (minheap[0].val< nums[index] - limit or minheap[0].index < leftIndex): temp = heapq.heappop(minheap) if (temp.index > leftIndex): leftIndex = temp.index if len(maxheap)> 0 and (-maxheap[0].val > nums[index] + limit): while len(maxheap) > 0 and (-maxheap[0].val > nums[index] + limit or maxheap[0].index < leftIndex ): temp = heapq.heappop(maxheap) if (temp.index > leftIndex): leftIndex = temp.index heapq.heappush(minheap, Item(nums[index], index)) heapq.heappush(maxheap, Item(-nums[index], index)) maxLen = max(maxLen, index - leftIndex) index += 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: minheap=[] maxheap=[] heapq.heapify(minheap) heapq.heapify(maxheap) heapq.heappush(minheap,[nums[0],0]) # add first element and its index heapq.heappush(maxheap,[-nums[0],0]) begin = 0 length = 1 end = 1 next_idx = 0 while begin <= end and end <= len(nums) - 1: if abs(minheap[0][0] - nums[end]) <= limit and abs(abs(maxheap[0][0]) - nums[end]) <= limit: length = max(length, (end - begin) + 1) else: while len(minheap) > 0 and abs(minheap[0][0] - nums[end]) > limit: ele,idx = heapq.heappop(minheap) next_idx = max(next_idx, idx) while len(maxheap) > 0 and abs(-maxheap[0][0] - nums[end]) > limit: ele,idx = heapq.heappop(maxheap) next_idx = max(next_idx, idx) begin = next_idx + 1 heapq.heappush(minheap, [nums[end],end]) heapq.heappush(maxheap, [-nums[end],end]) end+=1 return length # 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]) # maxindex=0 # while i<=j and j<=len(nums)-1: # if abs(minheap[0][0]-nums[j])<=limit and abs(abs(maxheap[0][0])-nums[j])<= limit: # length=max(length,j-i+1) # else: # while len(minheap)>0 and abs(minheap[0][0]-nums[j])>limit: # ele,index=heapq.heappop(minheap) # maxindex=max(maxindex,index) # while len(maxheap)>0 and abs(-maxheap[0][0]-nums[j])>limit: # ele,index=heapq.heappop(maxheap) # maxindex=max(maxindex,index) # 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 # def isValid(int_diff, j, max_value, min_value, limit, nums): # max_boundary = abs(max_value - nums[j]) # min_boundary = abs(min_value - nums[j]) # return int_diff <= limit and max_boundary <= limit and min_boundary <= limit # def find_error(int_diff,i, j, max_value, min_value, limit, nums,cache): # max_boundary = abs(max_value - nums[j]) # min_boundary = abs(min_value - nums[j]) # if int_diff > limit: return i # if max_boundary > limit: return cache[max_value] # if min_boundary > limit: return cache[min_value] # output = 1 # i = 0 # idx = 0 # cache = defaultdict(int) # while i < len(nums) and idx < len(nums)-1: # min_value = float('inf') # max_value = float('-inf') # cache[min_value] = i # cache[max_value] = i # for j in range(i,len(nums)): # int_diff = abs(nums[j] - nums[i]) # temp_min = min_value # temp_max = max_value # min_value = min(min_value, nums[j]) # max_value = max(max_value, nums[j]) # if temp_min != min_value: # cache[min_value] = j # if temp_max != min_value: # cache[max_value] = j # if isValid(int_diff, j, max_value, min_value, limit, nums): # output = max(output, (j - i) + 1) # else: # i = find_error(int_diff, i, j, max_value, min_value, limit, nums,cache) + 1 # break # idx = j # return output
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 MinMaxStack: def __init__(self): self.s = [] self.mins = [] self.maxes = [] def append(self, x): self.s.append(x) if not self.mins or x <= self.mins[-1]: self.mins.append(x) if not self.maxes or x >= self.maxes[-1]: self.maxes.append(x) def pop(self): x = self.s.pop() if x == self.mins[-1]: self.mins.pop() if x == self.maxes[-1]: self.maxes.pop() return x def min(self): return self.mins[-1] if self.mins else float('inf') def max(self): return self.maxes[-1] if self.maxes else -float('inf') def __len__(self): return len(self.s) class MinMaxQueue: def __init__(self): self.s1 = MinMaxStack() self.s2 = MinMaxStack() def enqueue(self, x): self.s1.append(x) def dequeue(self): if not self.s2: while self.s1: to_append = self.s1.pop() self.s2.append(to_append) return self.s2.pop() def min(self): return min(self.s1.min(), self.s2.min()) def max(self): return max(self.s1.max(), self.s2.max()) def __len__(self): return len(self.s1) + len(self.s2) class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 i = 0 max_len = 1 q = MinMaxQueue() q.enqueue(nums[0]) while i < len(nums): if q.max() - q.min() <= limit: max_len = max(max_len, len(q)) i += 1 if i < len(nums): q.enqueue(nums[i]) continue q.dequeue() 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 sortedcontainers import SortedList class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: start = 0 n = len(nums) k = limit s = SortedList() max_count = 0 for i in range(n): s.add(nums[i]) if s[-1] - s[0] > k: while s[-1] - s[0] > k: s.remove(nums[start]) start += 1 max_count = max(max_count, len(s)) return max_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: if len(nums) == 1: return 1 left = right = 0 min_n = max_n = nums[0] max_size = 0 while right < len(nums): if nums[right] > max_n: max_n = nums[right] if nums[right] < min_n: min_n = nums[right] while max_n - min_n > limit: left += 1 min_n = min(nums[left:right+1]) max_n = max(nums[left:right+1]) break if right - left > max_size: max_size = right - left right += 1 return max_size + 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: List[int], limit: int) -> int: minq = deque() maxq = deque() ret = l = 0 for i in range(len(nums)): while minq and nums[i]<minq[-1]: minq.pop() minq.append(nums[i]) while maxq and nums[i]>maxq[-1]: maxq.pop() maxq.append(nums[i]) while maxq[0]-minq[0]>limit: if nums[l]==maxq[0]: maxq.popleft() if nums[l]==minq[0]: minq.popleft() l+=1 ret = max(ret, i-l+1) return ret