Skip to content

Commit 855d65d

Browse files
committed
Add problem 2600: K Items With the Maximum Sum
1 parent 84a9979 commit 855d65d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,7 @@ pub mod problem_2594_minimum_time_to_repair_cars;
19231923
pub mod problem_2595_number_of_even_and_odd_bits;
19241924
pub mod problem_2596_check_knight_tour_configuration;
19251925
pub mod problem_2598_smallest_missing_non_negative_integer_after_operations;
1926+
pub mod problem_2600_k_items_with_the_maximum_sum;
19261927

19271928
#[cfg(test)]
19281929
mod test_utilities;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub mod modular_arithmetic;
2+
3+
pub trait Solution {
4+
fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: 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 = [((3, 2, 0, 2), 2), ((3, 2, 0, 4), 3), ((6, 6, 6, 13), 5)];
13+
14+
for ((num_ones, num_zeros, num_neg_ones, k), expected) in test_cases {
15+
assert_eq!(
16+
S::k_items_with_maximum_sum(num_ones, num_zeros, num_neg_ones, k),
17+
expected,
18+
);
19+
}
20+
}
21+
}
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 helper(num_ones: u32, num_zeros: u32, k: u32) -> u32 {
7+
k.min(num_ones) - k.saturating_sub(num_ones + num_zeros)
8+
}
9+
10+
pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {
11+
_ = num_neg_ones;
12+
13+
Self::helper(num_ones as _, num_zeros as _, k as _) as _
14+
}
15+
}
16+
17+
// ------------------------------------------------------ snip ------------------------------------------------------ //
18+
19+
impl super::Solution for Solution {
20+
fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {
21+
Self::k_items_with_maximum_sum(num_ones, num_zeros, num_neg_ones, k)
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
#[test]
28+
fn test_solution() {
29+
super::super::tests::run::<super::Solution>();
30+
}
31+
}

0 commit comments

Comments
 (0)