Skip to content

Commit c207880

Browse files
committed
feat: resolved
1 parent 8982c3a commit c207880

File tree

1 file changed

+4
-4
lines changed
  • number_of_subarrays_with_bounded_maximum/src

1 file changed

+4
-4
lines changed

number_of_subarrays_with_bounded_maximum/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ impl Solution {
44
pub fn num_subarray_bounded_max(nums: Vec<i32>, left: i32, right: i32) -> i32 {
55
let mut count = 0;
66
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 可以得到左侧的偏移量)
88
for right_boundary in 0..nums.len() {
99
if nums[right_boundary] > right {
1010
// subarray 中出现大于 right 的数字,销毁
1111
at_most_left = right_boundary as i32 + 1;
1212
left_boundary = right_boundary as i32 + 1;
1313
} else if nums[right_boundary] < left {
1414
// 从 left_boundary 到 at_most_left 之间移动左边界,形成的子集都符合要求。
15-
count += at_most_left - left_boundary + 1;
15+
count += at_most_left - left_boundary;
1616
} 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;
1919
}
2020
}
2121
count as i32

0 commit comments

Comments
 (0)