java.lang.Object
g1401_1500.s1438_longest_continuous_subarray_with_absolute_diff_less_than_or_equal_to_limit.Solution

public class Solution extends Object
1438 - Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.<p>Medium</p> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [8,2,4,7], limit = 4</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> All subarrays are:</p> <p>[8] with maximum absolute diff |8-8| = 0 <= 4.</p> <p>[8,2] with maximum absolute diff |8-2| = 6 > 4.</p> <p>[8,2,4] with maximum absolute diff |8-2| = 6 > 4.</p> <p>[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.</p> <p>[2] with maximum absolute diff |2-2| = 0 <= 4.</p> <p>[2,4] with maximum absolute diff |2-4| = 2 <= 4.</p> <p>[2,4,7] with maximum absolute diff |2-7| = 5 > 4.</p> <p>[4] with maximum absolute diff |4-4| = 0 <= 4.</p> <p>[4,7] with maximum absolute diff |4-7| = 3 <= 4.</p> <p>[7] with maximum absolute diff |7-7| = 0 <= 4.</p> <p>Therefore, the size of the longest subarray is 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0</p> <p><strong>Output:</strong> 3</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> <li><code>0 <= limit <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestSubarray

      public int longestSubarray(int[] nums, int limit)