File tree 1 file changed +4
-4
lines changed
number_of_subarrays_with_bounded_maximum/src
1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change @@ -4,18 +4,18 @@ impl Solution {
4
4
pub fn num_subarray_bounded_max ( nums : Vec < i32 > , left : i32 , right : i32 ) -> i32 {
5
5
let mut count = 0 ;
6
6
let mut left_boundary = 0 ;
7
- let mut at_most_left = 0 ; // 从左往右最后一个大于等于 left 的元素对应的索引
7
+ let mut at_most_left = 0 ; // 从左往右最后一个大于等于 left 的元素对应的索引右侧的索引(at_most_left - left_boundary 可以得到左侧的偏移量)
8
8
for right_boundary in 0 ..nums. len ( ) {
9
9
if nums[ right_boundary] > right {
10
10
// subarray 中出现大于 right 的数字,销毁
11
11
at_most_left = right_boundary as i32 + 1 ;
12
12
left_boundary = right_boundary as i32 + 1 ;
13
13
} else if nums[ right_boundary] < left {
14
14
// 从 left_boundary 到 at_most_left 之间移动左边界,形成的子集都符合要求。
15
- count += at_most_left - left_boundary + 1 ;
15
+ count += at_most_left - left_boundary;
16
16
} else {
17
- at_most_left = right_boundary as i32 ;
18
- count += at_most_left - left_boundary + 1 ;
17
+ at_most_left = right_boundary as i32 + 1 ;
18
+ count += at_most_left - left_boundary;
19
19
}
20
20
}
21
21
count as i32
You can’t perform that action at this time.
0 commit comments