Skip to content

Commit 3e961a3

Browse files
committed
Add problem 2500: Delete Greatest Value in Each Row
1 parent d3688f1 commit 3e961a3

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
@@ -1870,6 +1870,7 @@ pub mod problem_2492_minimum_score_of_a_path_between_two_cities;
18701870
pub mod problem_2496_maximum_value_of_a_string_in_an_array;
18711871
pub mod problem_2497_maximum_star_sum_of_a_graph;
18721872
pub mod problem_2498_frog_jump_ii;
1873+
pub mod problem_2500_delete_greatest_value_in_each_row;
18731874

18741875
#[cfg(test)]
18751876
mod test_utilities;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn delete_greatest_value(grid: Vec<Vec<i32>>) -> i32 {
7+
let mut grid = grid;
8+
9+
for row in &mut grid {
10+
row.sort_unstable();
11+
}
12+
13+
let columns = grid.first().map_or(0, Vec::len);
14+
15+
(0..columns).fold(0, |sum, i| sum + grid.iter().fold(i32::MIN, |max, row| max.max(row[i])))
16+
}
17+
}
18+
19+
// ------------------------------------------------------ snip ------------------------------------------------------ //
20+
21+
impl super::Solution for Solution {
22+
fn delete_greatest_value(grid: Vec<Vec<i32>>) -> i32 {
23+
Self::delete_greatest_value(grid)
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
#[test]
30+
fn test_solution() {
31+
super::super::tests::run::<super::Solution>();
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn delete_greatest_value(grid: Vec<Vec<i32>>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
use crate::test_utilities::Matrix;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [(&[[1, 2, 4], [3, 3, 1]] as &dyn Matrix<_>, 8), (&[[10]], 10)];
14+
15+
for (grid, expected) in test_cases {
16+
assert_eq!(S::delete_greatest_value(grid.to_vec()), expected);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)