3113. Find the Number of Subarrays Where Boundary Elements Are Maximum Hard

@problem@discussion
#Array#Binary Search#Stack#Monotonic Stack



1/**
2 * [3113] Find the Number of Subarrays Where Boundary Elements Are Maximum
3 *
4 * You are given an array of positive integers nums.
5 * Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of nums, where the first and the last elements of the subarray are equal to the largest element in the subarray.
6 *  
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [1,4,3,3,2]</span>
10 * Output: <span class="example-io">6</span>
11 * Explanation:
12 * There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
13 * 
14 * 	subarray [<u>1</u>,4,3,3,2], with its largest element 1. The first element is 1 and the last element is also 1.
15 * 	subarray [1,<u>4</u>,3,3,2], with its largest element 4. The first element is 4 and the last element is also 4.
16 * 	subarray [1,4,<u>3</u>,3,2], with its largest element 3. The first element is 3 and the last element is also 3.
17 * 	subarray [1,4,3,<u>3</u>,2], with its largest element 3. The first element is 3 and the last element is also 3.
18 * 	subarray [1,4,3,3,<u>2</u>], with its largest element 2. The first element is 2 and the last element is also 2.
19 * 	subarray [1,4,<u>3,3</u>,2], with its largest element 3. The first element is 3 and the last element is also 3.
20 * 
21 * Hence, we return 6.
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">nums = [3,3,3]</span>
26 * Output: <span class="example-io">6</span>
27 * Explanation:
28 * There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
29 * 
30 * 	subarray [<u>3</u>,3,3], with its largest element 3. The first element is 3 and the last element is also 3.
31 * 	subarray [3,<u>3</u>,3], with its largest element 3. The first element is 3 and the last element is also 3.
32 * 	subarray [3,3,<u>3</u>], with its largest element 3. The first element is 3 and the last element is also 3.
33 * 	subarray [<u>3,3</u>,3], with its largest element 3. The first element is 3 and the last element is also 3.
34 * 	subarray [3,<u>3,3</u>], with its largest element 3. The first element is 3 and the last element is also 3.
35 * 	subarray [<u>3,3,3</u>], with its largest element 3. The first element is 3 and the last element is also 3.
36 * 
37 * Hence, we return 6.
38 * </div>
39 * <strong class="example">Example 3:
40 * <div class="example-block">
41 * Input: <span class="example-io">nums = [1]</span>
42 * Output: <span class="example-io">1</span>
43 * Explanation:
44 * There is a single subarray of nums which is [<u>1</u>], with its largest element 1. The first element is 1 and the last element is also 1.
45 * Hence, we return 1.
46 * </div>
47 *  
48 * Constraints:
49 * 
50 * 	1 <= nums.length <= 10^5
51 * 	1 <= nums[i] <= 10^9
52 * 
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/find-the-number-of-subarrays-where-boundary-elements-are-maximum/
57// discuss: https://leetcode.com/problems/find-the-number-of-subarrays-where-boundary-elements-are-maximum/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61impl Solution {
62    pub fn number_of_subarrays(nums: Vec<i32>) -> i64 {
63        
64    }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_3113() {
75    }
76}
77


Back
© 2025 bowen.ge All Rights Reserved.