1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Medium
1/**
2 * [1438] Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
3 *
4 * 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.
5 *
6 * Example 1:
7 *
8 * Input: nums = [8,2,4,7], limit = 4
9 * Output: 2
10 * Explanation: All subarrays are:
11 * [8] with maximum absolute diff |8-8| = 0 <= 4.
12 * [8,2] with maximum absolute diff |8-2| = 6 > 4.
13 * [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
14 * [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
15 * [2] with maximum absolute diff |2-2| = 0 <= 4.
16 * [2,4] with maximum absolute diff |2-4| = 2 <= 4.
17 * [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
18 * [4] with maximum absolute diff |4-4| = 0 <= 4.
19 * [4,7] with maximum absolute diff |4-7| = 3 <= 4.
20 * [7] with maximum absolute diff |7-7| = 0 <= 4.
21 * Therefore, the size of the longest subarray is 2.
22 *
23 * Example 2:
24 *
25 * Input: nums = [10,1,2,4,7,2], limit = 5
26 * Output: 4
27 * Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
28 *
29 * Example 3:
30 *
31 * Input: nums = [4,2,2,2,4,4,2,2], limit = 0
32 * Output: 3
33 *
34 *
35 * Constraints:
36 *
37 * 1 <= nums.length <= 10^5
38 * 1 <= nums[i] <= 10^9
39 * 0 <= limit <= 10^9
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
45// discuss: https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn longest_subarray(nums: Vec<i32>, limit: i32) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_1438() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.