-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
pub fn grid_game(grid: Vec<Vec<i32>>) -> i64 { | ||
let [first_row, second_row]: &[_; 2] = grid.as_slice().try_into().ok().unwrap(); | ||
let first_row_sum = first_row.iter().fold(0_u64, |sum, &x| sum + u64::from(x as u32)); | ||
let mut result = u64::MAX; | ||
let mut top_sum = 0_u64; | ||
let mut bottom_sum = 0_u64; | ||
|
||
for (&top, &bottom) in first_row.iter().zip(second_row) { | ||
top_sum += u64::from(top as u32); | ||
result = result.min(bottom_sum.max(first_row_sum - top_sum)); | ||
bottom_sum += u64::from(bottom as u32); | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn grid_game(grid: Vec<Vec<i32>>) -> i64 { | ||
Self::grid_game(grid) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn grid_game(grid: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities::Matrix; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[2, 5, 4], [1, 5, 1]] as &dyn Matrix<_>, 4), | ||
(&[[3, 3, 1], [8, 5, 2]], 4), | ||
(&[[1, 3, 1, 15], [1, 3, 3, 1]], 7), | ||
]; | ||
|
||
for (grid, expected) in test_cases { | ||
assert_eq!(S::grid_game(grid.to_vec()), expected); | ||
} | ||
} | ||
} |