|
| 1 | +# [1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +title: "Finding the Longest Subarray with Absolute Diff Less than Limit" |
| 6 | +summary: "An efficient approach to finding the longest subarray with an absolute difference less than a specified limit using deques." |
| 7 | +date: "2023年06月23日" |
| 8 | +modified_date: "2023年06月23日" |
| 9 | +tags: ["algorithm", "sliding window", "deques", "javascript"] |
| 10 | +slug: "longest-subarray-absolute-diff" |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +# Intuition |
| 17 | + |
| 18 | +To solve this problem, the first step is to identify that we need to find a subarray where the difference between the maximum and minimum elements is less than or equal to a given limit. The problem can be effectively approached using a sliding window technique combined with deques to keep track of the maximum and minimum elements within the window. |
| 19 | + |
| 20 | +# Approach |
| 21 | + |
| 22 | +We use two deques: one to keep track of the maximum values and the other for the minimum values within the current window. The deques help us efficiently maintain the maximum and minimum values as we expand and contract the sliding window. |
| 23 | + |
| 24 | +1. Initialize two deques, `maxDeque` and `minDeque`, to store indices of array elements in decreasing and increasing order respectively. |
| 25 | +2. Iterate through the array with a sliding window defined by two pointers, `left` and `right`. |
| 26 | +3. For each element at `right`, update the deques to include this element while maintaining their order properties. |
| 27 | +4. Check if the current window satisfies the condition where the absolute difference between the maximum and minimum elements is less than or equal to the limit. If not, increment the `left` pointer to reduce the window size and maintain the condition. |
| 28 | +5. Keep track of the maximum window size that satisfies the condition. |
| 29 | + |
| 30 | +# Complexity |
| 31 | + |
| 32 | +- Time complexity: $$O(n)$$ |
| 33 | + The algorithm processes each element of the array at most twice, resulting in a linear time complexity. |
| 34 | + |
| 35 | +- Space complexity: $$O(n)$$ |
| 36 | + In the worst case, the deques can store all elements of the array, leading to a linear space complexity. |
| 37 | + |
| 38 | +# Code |
| 39 | + |
| 40 | +```javascript |
| 41 | +/** |
| 42 | + * @param {number[]} nums |
| 43 | + * @param {number} limit |
| 44 | + * @return {number} |
| 45 | + */ |
| 46 | +function longestSubarray(nums, limit) { |
| 47 | + let maxDeque = []; |
| 48 | + let minDeque = []; |
| 49 | + let left = 0; |
| 50 | + let result = 0; |
| 51 | + |
| 52 | + for (let right = 0; right < nums.length; right++) { |
| 53 | + // Maintain the maxDeque for the maximum value in the current window |
| 54 | + while ( |
| 55 | + maxDeque.length && |
| 56 | + nums[maxDeque[maxDeque.length - 1]] <= nums[right] |
| 57 | + ) { |
| 58 | + maxDeque.pop(); |
| 59 | + } |
| 60 | + maxDeque.push(right); |
| 61 | + |
| 62 | + // Maintain the minDeque for the minimum value in the current window |
| 63 | + while ( |
| 64 | + minDeque.length && |
| 65 | + nums[minDeque[minDeque.length - 1]] >= nums[right] |
| 66 | + ) { |
| 67 | + minDeque.pop(); |
| 68 | + } |
| 69 | + minDeque.push(right); |
| 70 | + |
| 71 | + // Check if the current window is valid |
| 72 | + while (nums[maxDeque[0]] - nums[minDeque[0]] > limit) { |
| 73 | + left++; |
| 74 | + if (left > maxDeque[0]) { |
| 75 | + maxDeque.shift(); |
| 76 | + } |
| 77 | + if (left > minDeque[0]) { |
| 78 | + minDeque.shift(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Update the result with the size of the current valid window |
| 83 | + result = Math.max(result, right - left + 1); |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | +} |
| 88 | +``` |
0 commit comments