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: 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 True: 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: window_multiset = SortedDict() start = 0 end = start ret = 0 while start < len(nums) and end < len(nums): if self.can_add(window_multiset, nums[end], limit): window_multiset[nums[end]] = window_multiset.get(nums[end], 0) + 1 end += 1 else: ret = max(end - start, ret) window_multiset[nums[end]] = window_multiset.get(nums[end], 0) + 1 while not self.is_valid_window(window_multiset, limit): self.remove_multiset(window_multiset, nums[start]) start += 1 end += 1 ret = max(end - start, ret) return ret def remove_multiset(self, window_multiset: SortedDict, num: int): window_multiset[num] -= 1 if not window_multiset[num]: window_multiset.pop(num) def is_valid_window(self, window_multiset: SortedDict, limit: int) -> bool: view = window_multiset.keys() return view[-1] - view[0] <= limit def can_add(self, window_multiset: SortedDict, cand: int, limit: int) -> bool: if not len(window_multiset): return True view = window_multiset.keys() return abs(cand - view[0]) <= limit and abs(cand - view[-1]) <= limit
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 # 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
import sortedcontainers class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: window = sortedcontainers.SortedList() N = len(nums) left = 0 right = -1 def check(): if not window: return True return window[-1] - window[0] <= limit ret = 1 while left < N and right < N: if not check(): window.remove(nums[left]) left += 1 else: ret = max(ret, right - left + 1) right += 1 if right < N: window.add(nums[right]) 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: i = 0 j = 0 max_length = 1 cur_max = cur_min = nums[0] while j < len(nums): dif = cur_max - cur_min if dif <= limit: max_length = j-i+1 j += 1 if j < len(nums): cur_max = max(cur_max,nums[j]) cur_min = min(cur_min,nums[j]) else: i += 1 j += 1 cur_max = max(nums[i:j+1]) cur_min = min(nums[i:j+1]) return max_length
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not len(nums): return 0 ml, start, end = 1, 0, 0 pqmin, pqmax = [], [] for end in range(len(nums)): heapq.heappush(pqmin, (nums[end], end)) heapq.heappush(pqmax, (-nums[end], end)) while start < end and abs(pqmax[0][0] + pqmin[0][0]) > limit:# we will pop out numbers until max - min < limit start += 1 while pqmax and pqmax[0][1] < start: heapq.heappop(pqmax) ## I only care if the max number index has been passed, if yes, we should pop out all numbers before that while pqmin and pqmin[0][1] < start: heapq.heappop(pqmin)## I only care if the min number index has been passed, if yes, we should pop out all numbers before that ml = max(ml, end - start + 1) return ml
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 = 1 start = 0 end = start + result mini = float('inf') maxi = float('-inf') while end <= len(nums): #step 1, check if the min and max has changed? #In this step we disgarded the first element, and added the last element mini = min(nums[start:end]) maxi = max(nums[start:end]) if abs(mini - maxi) > limit: start += 1 end += 1 else: # step2, the current condition is satisfied, explore the next element until # the limit is broken end += 1 while end <= len(nums): mini = min(nums[end-1], mini) maxi = max(nums[end-1], maxi) if abs(mini - maxi) > limit: result = max(result, end - start - 1) start += 1 end = start + result break else: end += 1 if end > len(nums): result = max(result, end - 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
from collections import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: N=len(nums) ans=0 maxi=deque() # decreasing 9 5 1 mini=deque() # increasing 1 5 9 j=0 for i in range(N): while mini and nums[i]<nums[mini[-1]]: mini.pop() while maxi and nums[i]>nums[maxi[-1]]: maxi.pop() mini.append(i) maxi.append(i) if abs(nums[maxi[0]]-nums[mini[0]])<=limit: ans=max(ans,abs(maxi[0]-mini[0])+1) else: # remove elements outside of the window while mini and mini[0]<=j: mini.popleft() while maxi and maxi[0]<=j: maxi.popleft() j+=1 return N-j
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: minQ = deque() maxQ = deque() ans = -1 l = 0 for r in range(len(nums)): while minQ and nums[r] < minQ[-1]: minQ.pop() while maxQ and nums[r] > maxQ[-1]: maxQ.pop() minQ.append(nums[r]) maxQ.append(nums[r]) while abs(maxQ[0]-minQ[0]) > limit: if minQ and minQ[0] == nums[l]: minQ.popleft() if maxQ and maxQ[0] == nums[l]: maxQ.popleft() 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 collections import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: asc = deque() dsc = deque() ret=l=0 for i in range(len(nums)): n=nums[i] while asc and n<asc[-1]: asc.pop() asc.append(n) while dsc and n>dsc[-1]: dsc.pop() dsc.append(n) while dsc[0]-asc[0]>limit: if dsc[0]==nums[l]: dsc.popleft() if asc[0]==nums[l]: asc.popleft() l+=1 ret = max(ret,i-l+1) 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
class Solution: # 520 ms def longestSubarray(self, nums, limit): # initialize maximum and minimum _min = _max = nums[0] count = 0 res, start =1, 0 for right in range(len(nums)): # update maximum and minimum if _min > nums[right]: _min = nums[right] if _max < nums[right]: _max = nums[right] # if the right bound valid, count and update result if _max - _min <= limit: count += 1 if res < count: res = count # if invalid, turn to find the valid left bound elif _max - _min > limit: # initialize maximum and minimum at this position _min = _max = nums[right] count = 1 left = right - 1 # keep updating until invalid while (left >= start and abs(_max - nums[left]) <= limit and abs(_min - nums[left]) <= limit): if _min > nums[left]: _min = nums[left] if _max < nums[left]: _max = nums[left] count += 1 left -= 1 # when ended, update the left bound to right+1 # because we have counted the \"right\" position now in while-loop above already start = right + 1 return res
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
from collections import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: asc = deque() dsc = deque() ret=l=0 for i in range(len(nums)): n=nums[i] while asc and n<asc[-1]: asc.pop() asc.append(n) while dsc and n>dsc[-1]: dsc.pop() dsc.append(n) while dsc[0]-asc[0]>limit: if nums[l]==dsc[0]: dsc.popleft() if nums[l]==asc[0]: asc.popleft() l+=1 ret = max(ret,i-l+1) 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: l = 0 min_dq = deque() max_dq = deque() out = 0 for r in range(len(nums)): val = nums[r] while min_dq and val < nums[min_dq[-1]]: min_dq.pop() while max_dq and val > nums[max_dq[-1]]: max_dq.pop() max_dq.append(r) min_dq.append(r) while nums[max_dq[0]]-nums[min_dq[0]] > limit: l+=1 if max_dq[0] < l: max_dq.popleft() if min_dq[0] < l: min_dq.popleft() out = max(out, r-l+1) return out
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 min_q = deque() max_q = deque() ans = 0 j = 0 for i, num in enumerate(nums): while min_q and min_q[-1][0] > num: min_q.pop() min_q.append((num, i)) while max_q and max_q[-1][0] < num: max_q.pop() max_q.append((num, i)) if abs(min_q[0][0] - max_q[0][0]) <= limit: ans = max(ans, i - j + 1) else: if min_q[0][1] < max_q[0][1]: j = min_q[0][1] + 1 min_q.popleft() elif min_q[0][1] > max_q[0][1]: j = max_q[0][1] + 1 max_q.popleft() else: j = max_q[0][1] + 1 min_q.popleft() max_q.popleft() # print(ans, min_q, max_q, j) 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: decStack = collections.deque() incStack = collections.deque() ans = 0 l = 0 for r, n in enumerate(nums): while decStack and decStack[-1][0] <= n: decStack.pop() decStack.append((n, r)) while incStack and incStack[-1][0] >= n: incStack.pop() incStack.append((n, r)) while decStack[0][0] - incStack[0][0] > limit: l = min(incStack[0][1], decStack[0][1]) + 1 while decStack and decStack[0][1] < l: decStack.popleft() while incStack and incStack[0][1] < l: incStack.popleft() ans = max(ans, r - l + 1) # print(ans, l, r, incStack, decStack) 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 collections class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: # sliding window mx, mn = collections.deque([0]), collections.deque([0]) ans = 1 left = 0 for i in range(1, len(nums)): # update max and min monotonic q while mx and nums[mx[-1]] < nums[i]: mx.pop() mx.append(i) while mn and nums[mn[-1]] > nums[i]: mn.pop() mn.append(i) while mx and mn and nums[mx[0]] - nums[mn[0]] > limit: left += 1 if mx[0] < left: mx.popleft() if mn[0] < left: mn.popleft() ans = max(ans, i-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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: maxq = collections.deque() minq = collections.deque() start = 0 for i, n in enumerate(nums): while maxq and maxq[-1][0] < n: maxq.pop() while minq and minq[-1][0] > n: minq.pop() maxq.append( [n, i] ) minq.append( [n, i] ) if maxq[0][0] - minq[0][0] > limit: if maxq[0][0] == nums[start]: maxq.popleft() if minq[0][0] == nums[start]: minq.popleft() start += 1 return len(nums) - start # res = 0 # maxq = [] # minq = [] # j = 0 # for i,n in enumerate(nums): # O(n) # heapq.heappush(maxq, [-n,i] ) # O(log n) # heapq.heappush(minq, [n,i] ) # while -maxq[0][0] - minq[0][0] > limit: # ind = min( maxq[0][1], minq[0][1] ) # while maxq[0][1] <= ind: # heapq.heappop(maxq) # O(log n) # while minq[0][1] <= ind: # heapq.heappop(minq) # j = ind+1 # res = max(res, i - j +1 ) # return res # res = 0 # i = 0 # arr = [] # space O(res) # for n in nums: # O(n) time # bisect.insort(arr, n) # O(res) # while arr[-1] - arr[0] > limit: # ind = bisect.bisect_right(arr, nums[i]) # O( log res ) # arr.pop(ind-1) # O(res) # i += 1 # res = max(res, len(arr)) # return res
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: max_elements = collections.deque() min_elements = collections.deque() max_len = 0 i = 0 for j, num in enumerate(nums): while max_elements and max_elements[-1] < num: max_elements.pop() while min_elements and min_elements[-1] > num: min_elements.pop() max_elements.append(num) min_elements.append(num) while max_elements[0] - min_elements[0] > limit: if max_elements and max_elements[0] == nums[i]: max_elements.popleft() if min_elements and min_elements[0] == nums[i]: min_elements.popleft() i += 1 max_len = max(max_len, j - i + 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 collections import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: mindeque = deque() maxdeque = deque() L = R = 0 res = 0 while R < len(nums): while mindeque and nums[mindeque[-1]] > nums[R]: mindeque.pop() mindeque.append(R) while maxdeque and nums[maxdeque[-1]] < nums[R]: maxdeque.pop() maxdeque.append(R) if nums[maxdeque[0]] - nums[mindeque[0]] <= limit: res = max(res, R - L + 1) else: while nums[maxdeque[0]] - nums[mindeque[0]] > limit: if L == maxdeque[0]: maxdeque.popleft() if L == mindeque[0]: mindeque.popleft() L += 1 R += 1 return res
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: min_deque, max_deque = deque(), deque() l = r = 0 ans = 0 while r < len(nums): while min_deque and nums[r] <= nums[min_deque[-1]]: min_deque.pop() while max_deque and nums[r] >= nums[max_deque[-1]]: max_deque.pop() min_deque.append(r) max_deque.append(r) while nums[max_deque[0]] - nums[min_deque[0]] > limit: l += 1 if l > min_deque[0]: min_deque.popleft() if l > max_deque[0]: max_deque.popleft() ans = max(ans, r - l + 1) r += 1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: minq, maxq = deque(), deque() i = 0 for num in nums: while minq and minq[-1] > num: minq.pop() while maxq and maxq[-1] < num: maxq.pop() minq.append(num) maxq.append(num) if maxq[0] - minq[0] > limit: if nums[i] == minq[0]: minq.popleft() if nums[i] == maxq[0]: maxq.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 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 maxQ and val >= nums[maxQ[-1]]: maxQ.pop() maxQ.append(i) while minQ and val - nums[minQ[0]] > limit: last = max(last, minQ.popleft()) while minQ and val <= nums[minQ[-1]]: minQ.pop() minQ.append(i) res = max(res, i - last) 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: from collections import deque asc, desc = deque(), deque() l, r = 0, 0 res = 0 while r < len(nums): while asc and nums[asc[-1]] >= nums[r]: asc.pop() while desc and nums[desc[-1]] <= nums[r]: desc.pop() asc.append(r) desc.append(r) while nums[desc[0]] - nums[asc[0]] > limit: l += 1 if desc[0] < l: desc.popleft() if asc[0] < l: asc.popleft() res = max(res, r - l + 1) r += 1 return res
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: min_deque, max_deque = deque(), deque() l = r = 0 ans = 0 while r < len(nums): while min_deque and nums[r] <= nums[min_deque[-1]]: min_deque.pop() while max_deque and nums[r] >= nums[max_deque[-1]]: max_deque.pop() min_deque.append(r) max_deque.append(r) while nums[max_deque[0]] - nums[min_deque[0]] > limit: l += 1 if l > min_deque[0]: min_deque.popleft() if l > max_deque[0]: max_deque.popleft() ans = max(ans, r-l + 1) r += 1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: mindeq=deque() maxdeq=deque() i=0 ans=1 for j in range(0,len(nums)): while(len(maxdeq)>0 and maxdeq[-1]<nums[j]): maxdeq.pop() maxdeq.append(nums[j]) while(len(mindeq)>0 and mindeq[-1]>nums[j]): mindeq.pop() mindeq.append(nums[j]) if(maxdeq[0]-mindeq[0]>limit): if(maxdeq[0]==nums[i]): maxdeq.popleft() if(mindeq[0]==nums[i]): mindeq.popleft() i+=1 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 = collections.deque() # monotonically decreasing mind = collections.deque() # monotonically increasing i = 0 # maxd[-1] gives last element and maxd[0] gives first/front element for num in nums: while maxd and num > maxd[-1]: maxd.pop() # biggr num eats up all smaller ones while mind and num < mind[-1]: mind.pop() maxd.append(num) mind.append(num) 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 # read - https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609708/Python-Clean-Monotonic-Queue-solution-with-detail-explanation-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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: min_deque, max_deque = deque(), deque() l = r = 0 ans = 0 while r < len(nums): while min_deque and nums[r] <= nums[min_deque[-1]]: min_deque.pop() while max_deque and nums[r] >= nums[max_deque[-1]]: max_deque.pop() min_deque.append(r) max_deque.append(r) if nums[max_deque[0]] - nums[min_deque[0]] > limit: while min_deque[0] <= l: min_deque.popleft() while max_deque[0] <= l: max_deque.popleft() l += 1 ans = max(ans, r - l + 1) r += 1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: # nums = [8,2,4,7] # max_len = 0 # first = 0 # second = 0 max_list = [] min_list = [] j = 0 for i in range(len(nums)): while max_list and max_list[-1] < nums[i]: max_list.pop() while min_list and min_list[-1] > nums[i]: min_list.pop() max_list.append(nums[i]) min_list.append(nums[i]) if max_list[0] - min_list[0] > limit: if max_list[0] == nums[j]: max_list = max_list[1:] if min_list[0] == nums[j]: min_list = min_list[1:] j += 1 return len(nums) - j
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums, limit): mini, maxi, res, i = deque([]), deque([]), 0, 0 for j in range(len(nums)): while mini and mini[-1][0] > nums[j]: mini.pop() mini.append((nums[j], j)) while maxi and maxi[-1][0] < nums[j]: maxi.pop() maxi.append((nums[j], j)) while abs(maxi[0][0] - mini[0][0]) > limit and i < j: if maxi[0][-1] <= i: maxi.popleft() elif mini[0][-1] <= i: mini.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: # keep track of max and min value of sliding window # mono decreasing deque max_deque = collections.deque() # mono increasing deque min_deque = collections.deque() n = len(nums) l = 0 res = 0 for r in range(n): self.push_max_deque(max_deque, nums, r) self.push_min_deque(min_deque, nums, r) # if current window violate the limit, # then it is guarantee current num change either # head of max_deque or min_deque while nums[max_deque[0]] - nums[min_deque[0]] > limit: if max_deque[0] == l: max_deque.popleft() if min_deque[0] == l: min_deque.popleft() l += 1 res = max(res, r - l + 1) return res def push_max_deque(self, max_deque, nums, i): while max_deque and nums[max_deque[-1]] < nums[i]: max_deque.pop() max_deque.append(i) def push_min_deque(self, min_deque, nums, i): while min_deque and nums[min_deque[-1]] > nums[i]: min_deque.pop() min_deque.append(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 collections import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 minq, maxq = deque(), deque() l = r = 0 ans = 0 while r < len(nums): while minq and nums[r] <= nums[minq[-1]]: minq.pop() while maxq and nums[r] >= nums[maxq[-1]]: maxq.pop() minq.append(r) maxq.append(r) while nums[maxq[0]] - nums[minq[0]] > limit: l += 1 if l > minq[0]: minq.popleft() if l > maxq[0]: maxq.popleft() ans = max(ans, r - l + 1) r += 1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: mini, maxi, res, i = deque([]), deque([]), 0, 0 for j in range(len(nums)): while mini and mini[-1][0] > nums[j]: mini.pop() mini.append((nums[j], j)) while maxi and maxi[-1][0] < nums[j]: maxi.pop() maxi.append((nums[j], j)) while abs(maxi[0][0] - mini[0][0]) > limit and i < j: if maxi[0][-1] <= i: maxi.popleft() elif mini[0][-1] <= i: mini.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: ans = 0 start_pos = 0 inc, dec = deque([]), deque([]) for i in range(len(nums)): while len(inc)>0 and nums[inc[-1]]>nums[i]: inc.pop() inc.append(i) while len(dec)>0 and nums[dec[-1]]<nums[i]: dec.pop() dec.append(i) while abs(nums[inc[0]]-nums[dec[0]])>limit: start_pos += 1 if inc[0]<start_pos: inc.popleft() if dec[0]<start_pos: dec.popleft() ans = max(ans, i-start_pos+1 ) # print(i,nums[i], inc,dec, start_pos, 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: i,j=0,0 nums.append(float('inf')) qmin=collections.deque() # monotonic non-decreasing qmax=collections.deque() # non-increasing qmin.append(0) qmax.append(0) ans=0 while j<len(nums)-1: while j<len(nums)-1 and nums[qmax[0]]-nums[qmin[0]]<=limit: ans = max(ans, j-i+1) j+=1 while qmax and nums[qmax[-1]]<nums[j]: qmax.pop() qmax.append(j) while qmin and nums[qmin[-1]]>nums[j]: qmin.pop() qmin.append(j) # only one can happen while qmax and nums[qmax[0]] - nums[j] > limit: i = qmax.popleft()+1 while qmin and nums[j] - nums[qmin[0]] > limit: i = qmin.popleft()+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: i,j=0,0 nums.append(float('inf')) qmin=collections.deque() # monotonic non-decreasing qmax=collections.deque() # non-increasing qmin.append(0) qmax.append(0) ans=0 while j<len(nums)-1: while j<len(nums)-1 and nums[qmax[0]]-nums[qmin[0]]<=limit: ans = max(ans, j-i+1) j+=1 while qmax and nums[qmax[-1]]<nums[j]: qmax.pop() qmax.append(j) while qmin and nums[qmin[-1]]>nums[j]: qmin.pop() qmin.append(j) # only one can happen while qmax and nums[qmax[0]] - nums[j] > limit: i = qmax.popleft()+1 while qmin and nums[j] - nums[qmin[0]] > limit: i = qmin.popleft()+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 queue import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if len(nums)==1: return 1 print(nums) decreasing=deque() increasing=deque() max_l=1 if abs(nums[1]-nums[0])<=limit:max_l=2 p0=0 p1=0 while p1<=len(nums)-1: while decreasing and nums[decreasing[-1]]<nums[p1]: decreasing.pop() while increasing and nums[increasing[-1]]>nums[p1]: increasing.pop() while decreasing and decreasing[0]<p0: decreasing.popleft() while increasing and increasing[0]<p0: increasing.popleft() decreasing.append(p1) increasing.append(p1) while p0<p1 and nums[decreasing[0]]-nums[increasing[0]]>limit: if decreasing[0]==p0:decreasing.popleft() if increasing[0]==p0:increasing.popleft() p0+=1 if nums[decreasing[0]]-nums[increasing[0]]<=limit:max_l=max(max_l,p1-p0+1) p1+=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, A: List[int], limit: int) -> int: maxd = collections.deque() mind = collections.deque() i = 0 for a in A: while maxd and a > maxd[-1]: maxd.pop() while mind and a < mind[-1]: mind.pop() maxd.append(a) mind.append(a) if maxd[0] - mind[0] > limit: if maxd[0] == A[i]: maxd.popleft() if mind[0] == A[i]: mind.popleft() i += 1 return len(A) - i
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
from collections import deque class Solution: def longestSubarray(self, nums, limit): mini, maxi, res, j = deque([]), deque([]), 0, 0 for i in range(len(nums)): while mini and mini[-1][0] > nums[i]: mini.pop() mini.append([nums[i], i]) while maxi and maxi[-1][0] < nums[i]: maxi.pop() maxi.append([nums[i], i]) while(maxi[0][0] - mini[0][0] > limit) and j < i : if mini[0][1] <= j: mini.popleft() elif maxi[0][1] <= j: maxi.popleft() j +=1 res = max(res, i-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: if len(nums) == 1 and limit >= 0: return 1 longest_subarray = 0 right = 0 left = 0 min_val = nums[left] max_val = nums[left] min_dq = [] max_dq = [] while right < len(nums): while len(min_dq) > 0 and min_dq[-1] > nums[right]: min_dq.pop() min_dq.append(nums[right]) while len(max_dq) > 0 and max_dq[-1] < nums[right]: max_dq.pop() max_dq.append(nums[right]) min_val = min_dq[0] max_val = max_dq[0] if abs(min_val - max_val) > limit: if min_dq[0] == nums[left]: min_dq.pop(0) if max_dq[0] == nums[left]: max_dq.pop(0) left += 1 longest_subarray = max(longest_subarray, right - left + 1) right += 1 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: ''' # Complexity of n^2 i = 0 j = 0 maxSize = 0 while j < len(nums): k = j - 1 # 1 while k >= i: if abs(nums[k] - nums[j]) > limit: i = k + 1 k -= 1 maxSize = max(maxSize, (j-i+1)) j += 1 return maxSize ''' sP = 0 eP = 0 minQ = collections.deque([]) maxQ = collections.deque([]) maxSize = 0 # maxS = 3 # ep - 4 # sp - 2 # 0 1 2 3 4 5 # 10 1 2 1 7 2 # minQ (3,1) # maxQ (4,7) while eP < len(nums): while minQ and minQ[-1][0] > nums[eP]: minQ.pop() minQ.append((nums[eP], eP)) while maxQ and maxQ[-1][0] < nums[eP]: maxQ.pop() maxQ.append((nums[eP],eP)) while abs(maxQ[0][0] - minQ[0][0]) > limit and sP < eP: if maxQ[0][1] <= sP: maxQ.popleft() elif minQ[0][1] <= sP: minQ.popleft() sP+=1 maxSize = max(maxSize, eP - sP + 1) eP +=1 return maxSize
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: i = 0 j = 0 bestSize = 0 maxVals = [] minVals = [] import heapq while j < len(nums): heappush(maxVals, (-nums[j], j)) heappush(minVals, (nums[j], j)) if -maxVals[0][0] - minVals[0][0] > limit: i = min(minVals[0][1], maxVals[0][1]) + 1 # this is the new i to start while minVals[0][1] < i: heappop(minVals) while maxVals[0][1] < i: heappop(maxVals) #print(nums[i:j+1], bestSize) bestSize = max(bestSize, j - i + 1) j += 1 return bestSize
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 def addMinStack(stack, val): while stack and stack[-1] > val: stack.pop() stack.append(val) def addMaxStack(stack, val): while stack and stack[-1] < val: stack.pop() stack.append(val) def delStack(stack, val): if stack and stack[0] == val: stack.popleft() class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 minstack = deque([]) maxstack = deque([]) start = 0 end = 0 maxSubArray = 0 while end < len(nums): addMinStack(minstack, nums[end]) addMaxStack(maxstack, nums[end]) if maxstack[0] - minstack[0] >limit: while maxstack[0] - minstack[0] > limit: delStack(minstack, nums[start]) delStack(maxstack, nums[start]) start += 1 maxSubArray = max(maxSubArray, end - start+1) end += 1 return maxSubArray
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: min_deque, max_deque = deque(), deque() l = r = 0 ans = 0 while r < len(nums): while min_deque and nums[r] <= nums[min_deque[-1]]: min_deque.pop() while max_deque and nums[r] >= nums[max_deque[-1]]: max_deque.pop() min_deque.append(r) max_deque.append(r) while nums[max_deque[0]] - nums[min_deque[0]] > limit: if l == min_deque[0]: min_deque.popleft() if l == max_deque[0]: max_deque.popleft() l += 1 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: List[int], limit: int) -> int: start = 0 end = 0 res = 0 max_q = deque() min_q = deque() while end < len(nums): while max_q and nums[max_q[-1]] <= nums[end]: max_q.pop() while min_q and nums[min_q[-1]] >= nums[end]: min_q.pop() max_q.append(end) min_q.append(end) # print(\"1\", max_q, min_q) while nums[max_q[0]] - nums[min_q[0]] > limit: start += 1 if start > max_q[0]: max_q.popleft() if start > min_q[0]: min_q.popleft() # print(\"2\", max_q, min_q) print((end - start + 1)) res = max(res, end - start + 1) end += 1 return res
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
from collections import deque class Solution: #revisit 09/09/2020 def longestSubarray(self, nums: List[int], limit: int) -> int: # for a window, find the window max and the window min # left and right pointer # use max_queue and min_queue to calculate the window max and window min N = len(nums) max_q, min_q = deque([]), deque([]) left, right = 0, 0 max_length = 0 while right < N: while max_q and max_q[-1][1] <= nums[right]: max_q.pop() max_q.append([right, nums[right]]) while min_q and min_q[-1][1] >= nums[right]: min_q.pop() min_q.append([right, nums[right]]) while max_q[0][1] - min_q[0][1] > limit: left += 1 if max_q[0][0] < left: max_q.popleft() if min_q[0][0] < left: min_q.popleft() max_length = max(max_length, right - left + 1) right += 1 return max_length # e.g. [10,1,2,4,7,2], limit = 5 # left, right =2,5, min_q = [(5,2)], max_q = [(4,7), (5,2)] max_lenght = 4 # from collections import deque # class Solution: # def longestSubarray(self, nums: List[int], limit: int) -> int: # N = len(nums) # if N == 1: # return 1 # result = 0 # min_q, max_q = deque([]), deque([]) # l, r = 0, 0 # while r < N: # while min_q and nums[min_q[-1]] >= nums[r]: # min_q.pop() # while max_q and nums[max_q[-1]] <= nums[r]: # max_q.pop() # min_q.append(r) # max_q.append(r) # while nums[max_q[0]] - nums[min_q[0]] > limit: # l += 1 # if max_q[0] < l: # max_q.popleft() # if min_q[0] < l: # min_q.popleft() # #print(f\"l = {l}, r = {r}, min_q = {min_q}, max_q = {max_q}\") # result = max(result, r - l + 1) # r += 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: n = len(nums) max_q = deque() #3,2,1,0 min_q = deque() #1,2,3,4 start = 0 ans = 0 for end in range(n): while max_q and max_q[-1] < nums[end]: max_q.pop() while min_q and min_q[-1] > nums[end]: min_q.pop() max_q.append(nums[end]) min_q.append(nums[end]) if max_q[0] - min_q[0] > limit: if max_q[0] == nums[start]: max_q.popleft() if min_q[0] == nums[start]: min_q.popleft() start += 1 ans = max(end-start+1, ans) return ans def longestSubarray2(self, nums: List[int], limit: int) -> int: #[10,1,2,4,7,2] #queue = [4, 7, 2, 8] #min_queue = [2, 8] #max_queue = [8] #limit: 5 #ans: 4 q = deque() min_q = deque() max_q = deque() ans = 0 for num in nums: q.append(num) while min_q and min_q[-1] > num: min_q.pop() min_q.append(num) while max_q and max_q[-1] < num: max_q.pop() max_q.append(num) while q and max_q[0] - min_q[0] > limit: del_num = q.popleft() if max_q[0] == del_num: max_q.popleft() if min_q[0] == del_num: min_q.popleft() ans = max(ans, len(q)) return ans def longestSubarray2(self, nums: List[int], limit: int) -> int: #[10,1,2,4,7,2] #queue = [4, 7, 2, 8] #min_queue = [2, 8] #max_queue = [8] #limit: 5 #ans: 4 min_q = deque() max_q = deque() start = 0 for num in nums: while min_q and min_q[-1] > num: min_q.pop() min_q.append(num) while max_q and max_q[-1] < num: max_q.pop() max_q.append(num) if max_q[0] - min_q[0] > limit: if max_q[0] == nums[start]: max_q.popleft() if min_q[0] == nums[start]: min_q.popleft() start += 1 return len(nums) - start def longestSubarray2(self, nums: List[int], limit: int) -> int: n = len(nums) min_dp = [[0 for _ in range(n)] for _ in range(n)] max_dp = [[0 for _ in range(n)] for _ in range(n)] ans = 1 if 0 <= limit else 0 for i in range(n): min_dp[i][i] = nums[i] max_dp[i][i] = nums[i] for k in range(2, n+1): for i in range(n-k+1): j = i + k - 1 min_dp[i][j] = min(min_dp[i][j-1], nums[j]) max_dp[i][j] = max(max_dp[i][j-1], nums[j]) if max_dp[i][j] - min_dp[i][j] <= limit: ans = k 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: List[int], limit: int) -> int: mx, mn = deque([0]),deque([0]) result = 1 l = 0 for i in range(1,len(nums)): while len(mx) != 0 and nums[mx[-1]] < nums[i]: mx.pop() mx.append(i) while len(mn) != 0 and nums[mn[-1]] > nums[i]: mn.pop() mn.append(i) while mx and mn and nums[mx[0]] -nums[mn[0]] > limit: l +=1 while mx[0] < l: mx.popleft() while mn[0] < l: mn.popleft() result = max(result, i-l+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
# 注意需要标记滑动窗口内min和max的取值, 否则有可能超时 # 这题需要使用单调栈或者最小最大堆 import heapq as hq class Solution: def longestSubarray(self, nums, limit): mini, maxi, res, i = deque([]), deque([]), 0, 0 for j in range(len(nums)): while mini and mini[-1][0] > nums[j]: mini.pop() mini.append((nums[j], j)) while maxi and maxi[-1][0] < nums[j]: maxi.pop() maxi.append((nums[j], j)) while abs(maxi[0][0] - mini[0][0]) > limit and i < j: if maxi[0][-1] <= i: maxi.popleft() elif mini[0][-1] <= i: mini.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: sd = collections.deque() ld = collections.deque() def smallpush(d, i): while d and d[-1][0] > nums[i]: d.pop() d.append((nums[i], i)) def largepush(d, i): while d and d[-1][0] < nums[i]: d.pop() d.append((nums[i], i)) def popleft(d, j): while d and d[0][1] < j: d.popleft() n = len(nums) ans = -math.inf start = 0 i = 0 while i < n: smallpush(sd, i) largepush(ld, i) if abs(ld[0][0] - sd[0][0]) <= limit: ans = max(ans, i - start + 1) else: # print(ld, sd, \" ------\") while abs(ld[0][0] - sd[0][0]) > limit: if ld[0][1] == start: ld.popleft() if sd[0][1] == start: sd.popleft() start += 1 # print(ld, sd) # start = min(ld[0][1], sd[0][1]) ans = max(ans, i - start + 1) i += 1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 maxd, mind = deque(), deque() res, j = 0, 0 for i in range(len(nums)): while len(maxd) > 0 and nums[maxd[-1]] < nums[i]: maxd.pop() while len(mind) > 0 and nums[mind[-1]] > nums[i]: mind.pop() maxd.append(i) mind.append(i) while j < i and len(maxd) > 0 and len(mind) > 0 and nums[maxd[0]] - nums[mind[0]] > limit: if j == maxd[0]: maxd.popleft() if j == mind[0]: mind.popleft() j += 1 res = max(res, i - 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
from collections import deque class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: lo = i = ans = 0 minq, maxq = deque(), deque() #(index, value) while i < len(nums): while minq and minq[-1][1] >= nums[i]: minq.pop() while maxq and maxq[-1][1] <= nums[i]: maxq.pop() minq.append((i, nums[i])) maxq.append((i, nums[i])) if maxq[0][1] - minq[0][1] > limit: lo += 1 if lo > minq[0][0]: minq.popleft() if lo > maxq[0][0]: maxq.popleft() else: ans = max(ans, i - lo + 1) i += 1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: ans = 0 min_deque, max_deque = collections.deque(), collections.deque() l = r = 0 while r < len(nums): while min_deque and nums[min_deque[-1]] >= nums[r]: min_deque.pop() while max_deque and nums[max_deque[-1]] <= nums[r]: max_deque.pop() min_deque.append(r) max_deque.append(r) while nums[max_deque[0]] - nums[min_deque[0]] > limit: l += 1 if max_deque[0] < l: max_deque.popleft() if min_deque[0] < l: min_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): 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
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: max_q = deque() min_q = deque() left = 0 result = 0 for i, n in enumerate(nums): while max_q and nums[max_q[-1]] <= n: max_q.pop() max_q.append(i) while min_q and nums[min_q[-1]] >= n: min_q.pop() min_q.append(i) while left < i and nums[max_q[0]] - nums[min_q[0]] > limit: if left == max_q[0]: max_q.popleft() elif left == min_q[0]: min_q.popleft() left += 1 result = max(result, i - left + 1) return result
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
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)): n = nums[i] while minq and n<minq[-1]: minq.pop() minq.append(n) while maxq and n>maxq[-1]: maxq.pop() maxq.append(n) while maxq[0]-minq[0]>limit: if minq[0]==nums[l]: minq.popleft() if maxq[0]==nums[l]: maxq.popleft() l+=1 ret = max(ret,i-l+1) 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
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: max_q = deque() min_q = deque() l = r = 0 ans = 0 while r < len(nums): num = nums[r] while max_q and nums[max_q[-1]] <= num: max_q.pop() while min_q and nums[min_q[-1]] >= num: min_q.pop() max_q.append(r) min_q.append(r) while nums[max_q[0]] - nums[min_q[0]] > limit: l += 1 if l > max_q[0]: max_q.popleft() if l > min_q[0]: min_q.popleft() ans = max(ans, r - l + 1) r+=1 return ans
Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.   Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. Example 2: Input: nums = [10,1,2,4,7,2], limit = 5 Output: 4 Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. Example 3: Input: nums = [4,2,2,2,4,4,2,2], limit = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 0 <= limit <= 10^9
import heapq class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: if not nums: return 0 max_len = 1 i, j = 0, 0 min_heap = [] max_heap = [] max_n = min_n = nums[0] while i <= j and j < len(nums): # print(i, j, max_len) if max_n-min_n <= limit: max_len = max(max_len, j-i+1) j+=1 if j == len(nums): continue if nums[j] > max_n: max_n = nums[j] heapq.heappush(min_heap, (nums[j], j)) elif nums[j] < min_n: min_n = nums[j] heapq.heappush(max_heap, (-nums[j], j)) else: heapq.heappush(min_heap, (nums[j], j)) heapq.heappush(max_heap, (-nums[j], j)) else: if nums[i] == max_n: while True: max_n, s = heapq.heappop(max_heap) if s > i: max_n *= -1 break elif nums[i] == min_n: while True: min_n, t = heapq.heappop(min_heap) if t > i: break i+=1 # print(i, j, max_len) # print('-------------') 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
#make every num in nums become right bound for the subarray once, and find the longest subarray. #i need to know the max and min value in the subarray, so I'll maintain a minHeap, maxHeap with (value, index), then I can alway cost O(logn) to get the max and min value. #O(NlogN) # class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: VALUE, INDEX = 0, 1 left = 0 min_h, max_h = [], [] max_len = 0 #nums = [8,2,4,7] for right, num in enumerate(nums): #right= 3, left = 2 heappush(min_h, (num, right)) #max = [(-7,3)(-4,2)(-2,1)] heappush(max_h, (-num, right)) #min = [(4,2)(7,3) (8,0)] #check whether the current subarray is valid #if it is invalid, pop out the extreme value while -max_h[0][VALUE] - min_h[0][VALUE] > limit: if max_h[0][INDEX] < min_h[0][INDEX]: left = heappop(max_h)[INDEX]+1 else: left = heappop(min_h)[INDEX]+1 #clean heap while min_h[0][INDEX] < left: heappop(min_h) while max_h[0][INDEX] < left: heappop(max_h) max_len = max(max_len, right-left+1) #max_len = 2 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: from heapq import heappop, heappush res = j = 0 m = [] n = [] for i in range(len(nums)): x = nums[i] heappush(m, (-x, i)) heappush(n, (x, i)) while abs(-m[0][0] - n[0][0]) > limit: j = min(m[0][1], n[0][1]) + 1 while j > m[0][1]: heappop(m) while j > n[0][1]: heappop(n) res = max(res, i - 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 = deque() minq = deque() k = 0 for i in nums: while maxq and i > maxq[-1]: maxq.pop() while minq and i < minq[-1]: minq.pop() maxq.append(i) minq.append(i) if maxq[0] - minq[0] > limit: if maxq[0] == nums[k]: maxq.popleft() if minq[0] == nums[k]: minq.popleft() k += 1 return len(nums)-k
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ low, high = 0, 0 for c in s: if c == "(": low += 1 high += 1 elif c == ")": if low > 0: low -= 1 high -= 1 else: if low > 0: low -= 1 high += 1 if high < 0: return False return low == 0
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ rem={} def dfs(i,c): if c<0: return False elif i==len(s): return not c elif (i,c) in rem: return rem[(i,c)] else: if s[i]=='(': rem[(i,c)]=dfs(i+1,c+1) elif s[i]==')': rem[(i,c)]=dfs(i+1,c-1) else: rem[(i,c)]=dfs(i+1,c) or dfs(i+1,c-1) or dfs(i+1,c+1) return rem[(i,c)] return dfs(0,0)
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ l = r = star = 0 for c in s: if c == "(": l+=1 if c == ")": r+=1 if c == "*": star+=1 if r > star + l: return False l = r = star = 0 for c in s[::-1]: if c == "(": l +=1 if c == ")": r +=1 if c == "*": star +=1 if l > star + r: return False return True
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ open = 0 star = 0 count = {'(' : 0, ')' : 0, '*' : 0} newS = [] for i in s: #count[i] += 1 if i == ')': if open > 0: open -= 1 del newS[len(newS) - 1 - newS[::-1].index('(')] elif star > 0: star -= 1 del newS[len(newS) - 1 - newS[::-1].index('*')] else: return False elif i == '(': open += 1 newS += '(' else: star += 1 newS += '*' if star >= open: o = 0 s = 0 for i in newS: if i == '(': o += 1 elif i == '*': if o > 0: o -= 1 if o != 0: return False else: return True else: return False
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ a=0 b=0 for i in range(len(s)) : # if s[i]== "*" : # The case of * a = a+1 #To account for its possibility to be the opening of a brace pair if b>0 : #To accout for its possibility to be the closing of a pair b = b-1 elif s[i]=="(" : #For each open brace you see, increment a and b (marking the potential to make a pair) a = a+1 b = b+1 elif s[i]==")" : a = a-1 #For each ) brace, decrement a - marking one complete pair. if b>0 : #This check is to ensure the terminating check - b==0 i.e. each ( has a pairing ). b = b-1 if a<0 : # If at any point the number of closing braces are more than opening braces, terminate return False if b==0 : # If the number of pairs are balanced, return true return True else : return False
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ cmin = cmax = 0 for i in s: if i == '(': cmax += 1 cmin += 1 if i == ')': cmax -= 1 cmin = max(cmin - 1, 0) if i == '*': cmax += 1 cmin = max(cmin - 1, 0) if cmax < 0: return False return cmin == 0
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): """ :type s: str :rtype: bool """ lo, hi = 0, 0 for c in s: lo += 1 if c == "(" else -1 hi += 1 if c != ")" else -1 if hi < 0: break lo = max(lo, 0) return lo == 0
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand. Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed. Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. Examples: Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty Note: You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
class Solution: def findMinStep(self, board, hand): """ :type board: str :type hand: str :rtype: int """ res=float("inf") hmap=collections.defaultdict(int) for c in hand: hmap[c]+=1 res=self.helper(board,hmap) if res == float("inf"): return -1 return res def helper(self,board,hmap): board=self.removeConsecutive(board) if len(board) ==0: return 0 cnt=float("inf") j=0 for i in range(len(board)+1): if i<len(board) and board[i] ==board[j]: continue need=3-(i-j) if hmap[board[j]]>=need: hmap[board[j]]-=need res=self.helper(board[0:j]+board[i:],hmap) if res!=float("inf"): cnt=min(cnt,res+need) hmap[board[j]]+=need j=i return cnt def removeConsecutive(self,board): j=0 for i in range(len(board)+1): if i<len(board) and board[i] ==board[j]: continue if i-j>=3: return self.removeConsecutive(board[0:j]+board[i:]) else: j=i return board
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand. Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed. Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. Examples: Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty Note: You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
class Solution: def findMinStep(self, board, hand): """ :type board: str :type hand: str :rtype: int """ return self.use_dfs(board, hand) def use_dfs(self, board, hand): counts = collections.Counter(hand) return self.dfs(board, counts) def dfs(self, board, hand): if not board: return 0 result = float('inf') i, j = 0, 0 n = len(board) while i < n: j = i while j < n and board[i] == board[j]: j += 1 color = board[i] insert = 3 - (j - i) if color in hand and hand[color] >= insert: new_board = self.shrink(board[:i] + board[j:]) hand[color] -= insert dist = self.dfs(new_board, hand) if dist >= 0: result = min(result, dist + insert) hand[color] += insert i = j return result if float('inf') != result else -1 def shrink(self, board): i, j = 0, 0 while i < len(board): j = i while j < len(board) and board[i] == board[j]: j += 1 if j - i >= 3: board = board[:i] + board[j:] i = 0 else: i = j return board
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand. Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed. Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. Examples: Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty Note: You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
class Solution: def findMinStep(self, board, hand): """ :type board: str :type hand: str :rtype: int """ return self.use_dfs(board, hand) def use_dfs(self, board, hand): counts = collections.Counter(hand) return self.dfs(board, counts) def dfs(self, board, hand): if not board: return 0 result = float('inf') i, j = 0, 0 n = len(board) while i < n: j = i while j < n and board[i] == board[j]: j += 1 color = board[i] remove = 3 - (j - i) if color in hand and hand[color] >= remove: new_board = self.shrink(board[:i] + board[j:]) hand[color] -= remove dist = self.dfs(new_board, hand) if dist >= 0: result = min(result, dist + remove) hand[color] += remove i = j return result if float('inf') != result else -1 def shrink(self, board): i, j = 0, 0 while i < len(board): j = i while j < len(board) and board[i] == board[j]: j += 1 if j - i >= 3: board = board[:i] + board[j:] i = 0 else: i = j return board
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand. Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed. Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. Examples: Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty Note: You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
class Solution: def findMinStep(self, board, hand): """ :type board: str :type hand: str :rtype: int """ hmap=collections.defaultdict(int) for c in hand: hmap[c]+=1 res=float("inf") res=self.helper(board,hmap) if res==float("inf"): return -1 return res def helper(self,board,hmap): board=self.removeContinues(board) if len(board) ==0: return 0 j=0 res=float("inf") for i in range(len(board)+1): if i<len(board) and board[i]==board[j]: continue need=3-(i-j) if hmap[board[j]]>=need: hmap[board[j]]-=need nxtCnt=self.helper(board[0:j]+board[i:],hmap) if nxtCnt!=float("inf"): res=min(res,nxtCnt+need) hmap[board[j]]+=need j=i return res def removeContinues(self,board): j=0 for i in range(len(board)+1): if i<len(board) and board[i]==board[j]: continue if i-j>=3: return self.removeContinues(board[0:j]+board[i:]) else: j=i return board
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: right = 0 ans = 0 for i in range(len(light)): if (light[i] > right): right = light[i] if (i + 1 == right): ans += 1 return ans
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
''' Time Limited https://leetcode.com/submissions/detail/406886943/testcase/ ''' class Solution: def numTimesAllBlue(self, light): cnt, N = 0, len(light) bulbs = [0] * N for i, pos in enumerate(light, start = 1): pos -= 1 if sum(bulbs[:pos]) == 2 * pos: bulbs[pos] = 2 while pos + 1 < N and bulbs[pos+1] == 1: bulbs[pos+1] = 2 pos += 1 if sum(bulbs) == 2 * i: cnt += 1 else: bulbs[pos] = 1 return cnt class Solution: def numTimesAllBlue(self, light): cnt, seen = 0, set() for v in light: seen.add(v) if max(seen) == len(seen): cnt += 1 return cnt class Solution: def numTimesAllBlue(self, light): cnt = 0 maxV, size = 0, 0 for v in light: size += 1 if v > maxV: maxV = v if maxV == size: cnt += 1 return cnt
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: def natural_sum(n): return (n*(n+1))//2 count, s = 0, 0 for i in range(len(light)): s += light[i] if natural_sum(i+1) == s: count += 1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: n = len(light) dp = [0] * (n+1) dp[0] = 2 max_so_far = moment = 0 for num in light: max_so_far = max(num, max_so_far) if dp[num-1] == 2: dp[num] = 2 for b in range(num+1, max_so_far+1): if dp[b] == 1: dp[b] = 2 else: break if dp[max_so_far] == 2: moment += 1 else: dp[num] = 1 return moment
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: res = hi = on = 0 for l in light: on += 1 if l>hi: hi = l if on==hi: res+=1 return res
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
import bisect class Solution: def numTimesAllBlue(self, light: List[int]) -> int: sl = [] count = 0 for k in range(len(light)): bisect.insort_right(sl,light[k]) if len(sl)==sl[-1]: count = count +1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: ans = 0 m = 0 for i, j in enumerate(light): m = max(m, j) if i + 1 == m: ans += 1 return ans
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: ans = 0 maxidx = 0 for i, l in enumerate(light): maxidx = max(maxidx, l) if i+1 == maxidx: ans += 1 return ans
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: history = [] count = 0 limit = -1 for i in light: history.append(light) limit = max(limit, i) if len(history) == limit: count += 1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: count=0 right=0 for i in range(len(light)): right = max(light[i], right) if right==i+1: count+=1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: h = [] heapq.heapify(h) ret = 0 for idx, l in enumerate(light): heapq.heappush(h, -l) max_val = -heapq.heappop(h) ret += max_val == idx + 1 heapq.heappush(h, -max_val) return ret
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: left_most_off = 0 right_most_on = -1 state = [0] * len(light) res = 0 for e in light: e -= 1 state[e] = 1 right_most_on = max(right_most_on, e) if e == left_most_off: while left_most_off < len(light) and state[left_most_off]: left_most_off += 1 if left_most_off == len(light) or left_most_off > right_most_on: res += 1 return res
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: cur_max=0 count=0 res=0 for i in light: cur_max=max(cur_max,i) count+=1 if count==cur_max: res+=1 return res
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: state = [] # on/off count = 0 blue = 0 last = 0 for i in range(0, len(light)): state.append(False) for i, k in enumerate(light): state[k-1] = True # count how many are blue for c in range(last, len(light)): if not state[c]: last = c break blue += 1 if blue == i+1: count += 1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: return sum(i == m for i, m in enumerate(itertools.accumulate(light, max), 1))
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: on = [False]*len(light) n = len(light) blue,ans = 0,0 for i in range(0,n): on[light[i]-1] = True while(blue<n and on[blue] == True): blue += 1 if(blue == i+1): ans += 1 return ans
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
from sortedcontainers import SortedList class Solution: def numTimesAllBlue(self, light: List[int]) -> int: turned = SortedList() ret = 0 for turn in light: turned.add(turn) if len(turned) == turned[-1]: ret += 1 return ret
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: n = len(light) mn, ma = n + 1, 0 ans = 0 for k, v in enumerate(light): ma, mn = max(ma, v), min(mn, v) if mn == 1 and ma - mn == k: ans += 1 return ans def numTimesAllBlue(self, light: List[int]) -> int: result, right = 0, 0 for i, num in enumerate(light, 1): right = max(right, num) result += (right == i) return result
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: count = 0 right = 0 target = 0 for n in reversed(light): right += 1 target = max(target, len(light) - n + 1) if right == target: count += 1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: arr = [0]*(len(light)+1) most_right = 0 count = 0 for i, num in enumerate(light): if num > most_right: arr[num] = arr[most_right]+1 most_right = num else: arr[most_right] += 1 if arr[most_right] == most_right: count += 1 return count
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: cnt = 0 checker = 0 light_status = 0 for i in range(0, len(light)): light_status |= 1 <<(light[i] - 1) checker |= 1 << i if checker == light_status: cnt+=1 return cnt
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: blue = right = 0 for i, a in enumerate(light, 1): right = max(right, a) if i == right: blue += 1 return blue
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: r=0 c=0 for i in range(len(light)): r=max(r,light[i]) if(r==i+1): c=c+1 return c
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: req = 1 seen = set() ans = 0 for i,n in enumerate(light, start=1): seen.add(n) if n == req: while req in seen: seen.remove(req) req += 1 if not seen: ans += 1 return ans
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: num = 0 maxPos = 0 res = [] for i in range(len(light)): maxPos = max(maxPos, light[i]) num += 1 if num == maxPos: res.append(i) return len(res)
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: bSum=0 pSum=0 rr=0 for i in range(len(light)): bSum+=light[i] pSum+=i+1 if bSum==pSum: rr+=1 return rr
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: max_so_far = 0 ret = 0 for i, l in enumerate(light): max_so_far = max(max_so_far, l) ret += max_so_far == i + 1 return ret
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light: List[int]) -> int: first = 0 last = 1 on = [False] * len(light) ans = 0 for l in light: # print(on, first, l, last) on[l-1] = True last = max(l, last) if first + 1 == l: while first<len(light) and on[first]: first += 1 if first == last: ans += 1 return ans
There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too. Return the number of moments in which all turned on bulbs are blue.   Example 1: Input: light = [2,1,3,5,4] Output: 3 Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4. Example 2: Input: light = [3,2,4,1,5] Output: 2 Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0). Example 3: Input: light = [4,1,2,3] Output: 1 Explanation: All bulbs turned on, are blue at the moment 3 (index-0). Bulb 4th changes to blue at the moment 3. Example 4: Input: light = [2,1,4,3,6,5] Output: 3 Example 5: Input: light = [1,2,3,4,5,6] Output: 6   Constraints: n == light.length 1 <= n <= 5 * 10^4 light is a permutation of  [1, 2, ..., n]
class Solution: def numTimesAllBlue(self, light): cnt = 0 maxV, size = 0, 0 for v in light: size += 1 if v > maxV: maxV = v if maxV == size: cnt += 1 return cnt