The title

portal

The text

Given an integer array nums and an integer limit, return the length of the longest continuous subarray in which the absolute difference between any two elements must be less than or equal to limit.

If no subarray exists, 0 is returned.

Example 1:

Enter nums = [8,2,4,7], limit = 4

Description: All subarrays are as follows: [8] the biggest absolute difference | | 8-8 = 0 < = 4. [8, 2] the biggest absolute difference | 2 | = 8-6 > 4. 4-trichlorobenzene [8] the biggest absolute difference | 2 | = 8-6 > 4.,2,4,7 [8] the biggest absolute difference | 2 | = 8-6 > 4. [2] Biggest absolute difference | 2-2 | = 0 < = 4. [2, 4] biggest absolute difference | | 2-4 = 2 < = 4.,4,7 [2] the biggest absolute difference | 2-7 | = 5 > 4. [4] the biggest absolute difference | | 4-4 = 0 < = 4. [4, 7] biggest absolute difference 4-7 | | = 3 < = 4. [7] the biggest absolute difference | 7-7 | = 0 < = 4. Therefore, the largest array that satisfies the problem has a length of 2.

Example 2:

Enter nums = [10,1,2,4,7,2], limit = 5

Output: 4: meet the question of the array is the firstborn,4,7,2 [2], the biggest absolute difference | 2-7 | = 5 < = 5.

Example 3:

Enter: nums = [4,2,2, 4,4,2,2], limit = 0

Output: 3

Tip:

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
Copy the code

Source: LeetCode

The template

int longestSubarray(int* nums, int numsSize, int limit){}Copy the code

The problem solving

Analysis of the

A. the b. the C. the D. the

We need to compare the absolute difference of the contiguous subarray of the given array (the largest element minus the smallest element) with the given Limit and return the length of the longest contiguous subarray whose absolute difference <= Limit

So understand the process:

First == selects the subarray ==

Then == get absolute difference == then == compare limit== finally == select the next subarray ==

And then add as many optimizations as I can think of

You don’t have to compare any of the lengths that have already been compared