|
| 1 | +--- |
| 2 | +id: longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit |
| 3 | +title: Longest Continuous Subarray with Absolute Diff Less Than or Equal to Limit |
| 4 | +level: hard |
| 5 | +sidebar_label: Longest Continuous Subarray with Absolute Diff Less Than or Equal to Limit |
| 6 | +tags: |
| 7 | + - Array |
| 8 | + - Sliding Window |
| 9 | + - Deque |
| 10 | + - Java |
| 11 | +description: "This document provides solutions for the Longest Continuous Subarray with Absolute Diff Less Than or Equal to Limit problem." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Statement |
| 15 | + |
| 16 | +Given an array of integers `nums` and an integer `limit`, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to `limit`. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +Input: `nums = [8,2,4,7]`, `limit = 4` |
| 21 | + |
| 22 | +Output: `2` |
| 23 | + |
| 24 | +Explanation: All subarrays are: |
| 25 | +- `[8]` with maximum absolute diff `|8-8| = 0 <= 4`. |
| 26 | +- `[8,2]` with maximum absolute diff `|8-2| = 6 > 4`. |
| 27 | +- `[8,2,4]` with maximum absolute diff `|8-2| = 6 > 4`. |
| 28 | +- `[8,2,4,7]` with maximum absolute diff `|8-2| = 6 > 4`. |
| 29 | +- `[2]` with maximum absolute diff `|2-2| = 0 <= 4`. |
| 30 | +- `[2,4]` with maximum absolute diff `|2-4| = 2 <= 4`. |
| 31 | +- `[2,4,7]` with maximum absolute diff `|2-7| = 5 > 4`. |
| 32 | +- `[4]` with maximum absolute diff `|4-4| = 0 <= 4`. |
| 33 | +- `[4,7]` with maximum absolute diff `|4-7| = 3 <= 4`. |
| 34 | +- `[7]` with maximum absolute diff `|7-7| = 0 <= 4`. |
| 35 | + |
| 36 | +Therefore, the size of the longest subarray is 2. |
| 37 | + |
| 38 | +**Example 2:** |
| 39 | + |
| 40 | +Input: `nums = [10,1,2,4,7,2]`, `limit = 5` |
| 41 | + |
| 42 | +Output: `4` |
| 43 | + |
| 44 | +Explanation: The subarray `[2,4,7,2]` is the longest since the maximum absolute diff is `|2-7| = 5 <= 5`. |
| 45 | + |
| 46 | +**Example 3:** |
| 47 | + |
| 48 | +Input: `nums = [4,2,2,2,4,4,2,2]`, `limit = 0` |
| 49 | + |
| 50 | +Output: `3` |
| 51 | + |
| 52 | +**Constraints:** |
| 53 | + |
| 54 | +- `1 <= nums.length <= 10^5` |
| 55 | +- `1 <= nums[i] <= 10^9` |
| 56 | +- `0 <= limit <= 10^9` |
| 57 | + |
| 58 | +## Solutions |
| 59 | + |
| 60 | +### Approach |
| 61 | + |
| 62 | +To determine the length of the longest subarray where the absolute difference between any two elements is less than or equal to `limit`, follow these steps: |
| 63 | + |
| 64 | +1. **Sliding Window with Deque:** |
| 65 | + - Use two deques to maintain the maximum and minimum values in the current window. |
| 66 | + - Slide the window across the array and adjust the window size to ensure the absolute difference condition is met. |
| 67 | + |
| 68 | +### Java |
| 69 | + |
| 70 | +```java |
| 71 | + |
| 72 | + |
| 73 | +class Solution { |
| 74 | + public int longestSubarray(int[] nums, int limit) { |
| 75 | + LinkedList<Integer> increase = new LinkedList<>(); |
| 76 | + LinkedList<Integer> decrease = new LinkedList<>(); |
| 77 | + |
| 78 | + int max = 0; |
| 79 | + int left = 0; |
| 80 | + |
| 81 | + for (int i = 0; i < nums.length; i++) { |
| 82 | + int n = nums[i]; |
| 83 | + |
| 84 | + while (!increase.isEmpty() && n < increase.getLast()) { |
| 85 | + increase.removeLast(); |
| 86 | + } |
| 87 | + increase.add(n); |
| 88 | + |
| 89 | + while (!decrease.isEmpty() && n > decrease.getLast()) { |
| 90 | + decrease.removeLast(); |
| 91 | + } |
| 92 | + decrease.add(n); |
| 93 | + |
| 94 | + while (decrease.getFirst() - increase.getFirst() > limit) { |
| 95 | + if (nums[left] == decrease.getFirst()) { |
| 96 | + decrease.removeFirst(); |
| 97 | + } |
| 98 | + if (nums[left] == increase.getFirst()) { |
| 99 | + increase.removeFirst(); |
| 100 | + } |
| 101 | + left++; |
| 102 | + } |
| 103 | + |
| 104 | + int size = i - left + 1; |
| 105 | + max = Math.max(max, size); |
| 106 | + } |
| 107 | + |
| 108 | + return max; |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | +### Python |
| 113 | +```Python |
| 114 | +class Solution: |
| 115 | + def longestSubarray(self, nums: List[int], limit: int) -> int: |
| 116 | + increase = deque() |
| 117 | + decrease = deque() |
| 118 | + max_length = 0 |
| 119 | + left = 0 |
| 120 | + |
| 121 | + for i in range(len(nums)): |
| 122 | + n = nums[i] |
| 123 | + |
| 124 | + while increase and n < increase[-1]: |
| 125 | + increase.pop() |
| 126 | + increase.append(n) |
| 127 | + |
| 128 | + while decrease and n > decrease[-1]: |
| 129 | + decrease.pop() |
| 130 | + decrease.append(n) |
| 131 | + |
| 132 | + while decrease[0] - increase[0] > limit: |
| 133 | + if nums[left] == decrease[0]: |
| 134 | + decrease.popleft() |
| 135 | + if nums[left] == increase[0]: |
| 136 | + increase.popleft() |
| 137 | + left += 1 |
| 138 | + |
| 139 | + max_length = max(max_length, i - left + 1) |
| 140 | + |
| 141 | + return max_length |
| 142 | +``` |
0 commit comments