Skip to content

Commit 58a1f29

Browse files
committed
Add problem 2958: Length of Longest Subarray With at Most K Frequency
1 parent 66b71c2 commit 58a1f29

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,7 @@ pub mod problem_2952_minimum_number_of_coins_to_be_added;
20282028
pub mod problem_2956_find_common_elements_between_two_arrays;
20292029
pub mod problem_2957_remove_adjacent_almost_equal_characters;
20302030
pub mod problem_2958_length_of_longest_subarray_with_at_most_k_frequency;
2031+
pub mod problem_2960_count_tested_devices_after_test_operations;
20312032

20322033
#[cfg(test)]
20332034
mod test_utilities;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn count_tested_devices(battery_percentages: Vec<i32>) -> i32 {
7+
let mut result = 0;
8+
9+
for &battery in &battery_percentages {
10+
result += i32::from(battery - result > 0);
11+
}
12+
13+
result
14+
}
15+
}
16+
17+
// ------------------------------------------------------ snip ------------------------------------------------------ //
18+
19+
impl super::Solution for Solution {
20+
fn count_tested_devices(battery_percentages: Vec<i32>) -> i32 {
21+
Self::count_tested_devices(battery_percentages)
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
#[test]
28+
fn test_solution() {
29+
super::super::tests::run::<super::Solution>();
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn count_tested_devices(battery_percentages: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[1, 1, 2, 1, 3] as &[_], 3), (&[0, 1, 2], 2)];
13+
14+
for (battery_percentages, expected) in test_cases {
15+
assert_eq!(S::count_tested_devices(battery_percentages.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)