2762. Continuous Subarrays Medium

@problem@discussion
#Array#Queue#Sliding Window#Heap (Priority Queue)#Ordered Set#Monotonic Queue



1/**
2 * [2762] Continuous Subarrays
3 *
4 * You are given a 0-indexed integer array nums. A subarray of nums is called continuous if:
5 * 
6 * 	Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, <font face="monospace">0 <=</font> |nums[i1] - nums[i2]| <= 2.
7 * 
8 * Return the total number of continuous subarrays.
9 * A subarray is a contiguous non-empty sequence of elements within an array.
10 *  
11 * <strong class="example">Example 1:
12 * 
13 * Input: nums = [5,4,2,4]
14 * Output: 8
15 * Explanation: 
16 * Continuous subarray of size 1: [5], [4], [2], [4].
17 * Continuous subarray of size 2: [5,4], [4,2], [2,4].
18 * Continuous subarray of size 3: [4,2,4].
19 * There are no subarrys of size 4.
20 * Total continuous subarrays = 4 + 3 + 1 = 8.
21 * It can be shown that there are no more continuous subarrays.
22 * 
23 *  
24 * <strong class="example">Example 2:
25 * 
26 * Input: nums = [1,2,3]
27 * Output: 6
28 * Explanation: 
29 * Continuous subarray of size 1: [1], [2], [3].
30 * Continuous subarray of size 2: [1,2], [2,3].
31 * Continuous subarray of size 3: [1,2,3].
32 * Total continuous subarrays = 3 + 2 + 1 = 6.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= nums.length <= 10^5
38 * 	1 <= nums[i] <= 10^9
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/continuous-subarrays/
44// discuss: https://leetcode.com/problems/continuous-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn continuous_subarrays(nums: Vec<i32>) -> i64 {
50        
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_2762() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.