LeetCode 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

Description

Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.

 

Example 1:

Input: nums = [8,2,4,7], limit = 4
Output: 2 
Explanation: All subarrays are: 
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4. 
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4. 
Therefore, the size of the longest subarray is 2.

Example 2:

Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4 
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.

Example 3:

Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 0 <= limit <= 109

Solutions

Solution 1: Ordered Set + Sliding Window

We can enumerate each position as the right endpoint of the subarray, and find the leftmost left endpoint corresponding to it, such that the difference between the maximum and minimum values in the interval does not exceed limit. During the process, we use an ordered set to maintain the maximum and minimum values within the window.

The time complexity is O(n log n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: sl = SortedList() ans = j = 0 for i, x in enumerate(nums): sl.add(x) while sl[-1] - sl[0] > limit: sl.remove(nums[j]) j += 1 ans = max(ans, i - j + 1) return ans(code-box)

Solution 2: Binary Search + Sliding Window

We notice that if a subarray of length k satisfies the condition, then a subarray of length k' < k also satisfies the condition. This shows a monotonicity, therefore, we can use binary search to find the longest subarray that satisfies the condition.

We define the left boundary of the binary search as l = 0, and the right boundary as r = n. For each mid = l + r + 12, we check whether there exists a subarray of length mid that satisfies the condition. If it exists, we update l = mid, otherwise we update r = mid - 1. The problem is transformed into whether there exists a subarray of length mid in the array that satisfies the condition, which is actually to find the difference between the maximum and minimum values in the sliding window does not exceed limit. We can use two monotonic queues to maintain the maximum and minimum values in the window respectively.

The time complexity is O(n × log n), and the space complexity is O(n). Where n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: def check(k: int) -> bool: min_q = deque() max_q = deque() for i, x in enumerate(nums): if min_q and i - min_q[0] + 1 > k: min_q.popleft() if max_q and i - max_q[0] + 1 > k: max_q.popleft() while min_q and nums[min_q[-1]] >= x: min_q.pop() while max_q and nums[max_q[-1]] <= x: max_q.pop() min_q.append(i) max_q.append(i) if i >= k - 1 and nums[max_q[0]] - nums[min_q[0]] <= limit: return True return False l, r = 1, len(nums) while l < r: mid = (l + r + 1) >> 1 if check(mid): l = mid else: r = mid - 1 return l(code-box)

Solution 3: Sliding Window + Deque

We can use a deque to maintain the maximum and minimum values within the window. We maintain two deques, one for storing the indices of the maximum values and the other for the minimum values within the window. Define two pointers l and r to point to the left and right boundaries of the window, respectively.

Each time we move the right boundary r to the right, we check if the element corresponding to the tail index of the maximum value deque is less than the current element. If it is, we dequeue the tail element until the element corresponding to the tail of the maximum value deque is not less than the current element. Similarly, we check if the element corresponding to the tail index of the minimum value deque is greater than the current element. If it is, we dequeue the tail element until the element corresponding to the tail of the minimum value deque is not greater than the current element. Then, we enqueue the current element's index.

If the difference between the elements at the front of the maximum value deque and the minimum value deque is greater than limit, then we move the left boundary l to the right. If the element at the front of the maximum value deque is less than l, we dequeue the front element of the maximum value deque. Similarly, if the element at the front of the minimum value deque is less than l, we dequeue the front element of the minimum value deque.

The answer is n - l.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def longestSubarray(self, nums: List[int], limit: int) -> int: maxq = deque() minq = deque() l, n = 0, len(nums) for r, x in enumerate(nums): while maxq and nums[maxq[-1]] < x: maxq.pop() while minq and nums[minq[-1]] > x: minq.pop() maxq.append(r) minq.append(r) if nums[maxq[0]] - nums[minq[0]] > limit: l += 1 if maxq[0] < l: maxq.popleft() if minq[0] < l: minq.popleft() return n - l(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !