Skip to content

Commit 8982c3a

Browse files
committed
feat: submission_2 unresolved
1 parent 8559963 commit 8982c3a

File tree

1 file changed

+24
-11
lines changed
  • number_of_subarrays_with_bounded_maximum/src

1 file changed

+24
-11
lines changed

number_of_subarrays_with_bounded_maximum/src/lib.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ pub struct Solution {}
33
impl Solution {
44
pub fn num_subarray_bounded_max(nums: Vec<i32>, left: i32, right: i32) -> i32 {
55
let mut count = 0;
6+
let mut left_boundary = 0;
67
let mut at_most_left = 0; // 从左往右最后一个大于等于 left 的元素对应的索引
7-
let mut subarray_left = 0;
8-
for i in 0..nums.len() {
9-
if nums[i] > right {
10-
count += at_most_left - subarray_left;
11-
at_most_left = i + 1;
12-
subarray_left = i + 1;
13-
continue;
14-
}
15-
count += 1;
16-
if nums[i] >= left {
17-
at_most_left = i;
8+
for right_boundary in 0..nums.len() {
9+
if nums[right_boundary] > right {
10+
// subarray 中出现大于 right 的数字,销毁
11+
at_most_left = right_boundary as i32 + 1;
12+
left_boundary = right_boundary as i32 + 1;
13+
} else if nums[right_boundary] < left {
14+
// 从 left_boundary 到 at_most_left 之间移动左边界,形成的子集都符合要求。
15+
count += at_most_left - left_boundary + 1;
16+
} else {
17+
at_most_left = right_boundary as i32;
18+
count += at_most_left - left_boundary + 1;
1819
}
1920
}
2021
count as i32
@@ -48,4 +49,16 @@ mod tests {
4849
expected
4950
);
5051
}
52+
53+
#[test]
54+
fn submission_2() {
55+
let nums = [16, 69, 88, 85, 79, 87, 37, 33, 39, 34];
56+
let left = 55;
57+
let right = 57;
58+
let expected = 0;
59+
assert_eq!(
60+
Solution::num_subarray_bounded_max(nums.to_vec(), left, right),
61+
expected
62+
);
63+
}
5164
}

0 commit comments

Comments
 (0)