Skip to content

Commit e111a69

Browse files
committed
Add problem 3285: Find Indices of Stable Mountains
1 parent beb6b08 commit e111a69

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,7 @@ pub mod problem_3275_k_th_nearest_obstacle_queries;
21692169
pub mod problem_3280_convert_date_to_binary;
21702170
pub mod problem_3281_maximize_score_of_numbers_in_ranges;
21712171
pub mod problem_3282_reach_end_of_array_with_max_score;
2172+
pub mod problem_3285_find_indices_of_stable_mountains;
21722173
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21732174

21742175
#[cfg(test)]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn stable_mountains(height: Vec<i32>, threshold: i32) -> Vec<i32> {
7+
let mut height = height;
8+
let mut prev = 0;
9+
let mut i = -1;
10+
11+
height.retain_mut(|slot| {
12+
i += 1;
13+
14+
let keep = prev > threshold;
15+
16+
prev = *slot;
17+
18+
if keep {
19+
*slot = i;
20+
}
21+
22+
keep
23+
});
24+
25+
height
26+
}
27+
}
28+
29+
// ------------------------------------------------------ snip ------------------------------------------------------ //
30+
31+
impl super::Solution for Solution {
32+
fn stable_mountains(height: Vec<i32>, threshold: i32) -> Vec<i32> {
33+
Self::stable_mountains(height, threshold)
34+
}
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
#[test]
40+
fn test_solution() {
41+
super::super::tests::run::<super::Solution>();
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn stable_mountains(height: Vec<i32>, threshold: i32) -> Vec<i32>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
use crate::test_utilities;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [
14+
((&[1, 2, 3, 4, 5] as &[_], 2), &[3, 4] as &[_]),
15+
((&[10, 1, 10, 1, 10], 3), &[1, 3]),
16+
((&[10, 1, 10, 1, 10], 10), &[]),
17+
];
18+
19+
for ((height, threshold), expected) in test_cases {
20+
assert_eq!(
21+
test_utilities::unstable_sorted(S::stable_mountains(height.to_vec(), threshold)),
22+
expected,
23+
);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)