|
| 1 | +<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p> |
| 2 | + |
| 3 | +<p> </p> |
| 4 | +<p><strong class="example">Example 1:</strong></p> |
| 5 | + |
| 6 | +<pre> |
| 7 | +<strong>Input:</strong> nums = [8,2,4,7], limit = 4 |
| 8 | +<strong>Output:</strong> 2 |
| 9 | +<strong>Explanation:</strong> All subarrays are: |
| 10 | +[8] with maximum absolute diff |8-8| = 0 <= 4. |
| 11 | +[8,2] with maximum absolute diff |8-2| = 6 > 4. |
| 12 | +[8,2,4] with maximum absolute diff |8-2| = 6 > 4. |
| 13 | +[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. |
| 14 | +[2] with maximum absolute diff |2-2| = 0 <= 4. |
| 15 | +[2,4] with maximum absolute diff |2-4| = 2 <= 4. |
| 16 | +[2,4,7] with maximum absolute diff |2-7| = 5 > 4. |
| 17 | +[4] with maximum absolute diff |4-4| = 0 <= 4. |
| 18 | +[4,7] with maximum absolute diff |4-7| = 3 <= 4. |
| 19 | +[7] with maximum absolute diff |7-7| = 0 <= 4. |
| 20 | +Therefore, the size of the longest subarray is 2. |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5 |
| 27 | +<strong>Output:</strong> 4 |
| 28 | +<strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong class="example">Example 3:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0 |
| 35 | +<strong>Output:</strong> 3 |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 43 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 44 | + <li><code>0 <= limit <= 10<sup>9</sup></code></li> |
| 45 | +</ul> |
0 commit comments