-
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.
Add problem 1937: Maximum Number of Points with Cost
- Loading branch information
Showing
3 changed files
with
72 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
49 changes: 49 additions & 0 deletions
49
src/problem_1937_maximum_number_of_points_with_cost/dynamic_programming.rs
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,49 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::mem; | ||
|
||
impl Solution { | ||
pub fn max_points(points: Vec<Vec<i32>>) -> i64 { | ||
let columns = points.first().map_or(0, Vec::len); | ||
let mut buffer = vec![0_u64; columns * 2].into_boxed_slice(); | ||
let (mut cache, mut temp) = buffer.split_at_mut(columns); | ||
|
||
for row in points { | ||
let mut left_max = 0; | ||
|
||
for (i, (target, &top)) in (0..).zip(temp.iter_mut().zip(&*cache)) { | ||
left_max = left_max.max(top + i); | ||
*target = left_max - i; | ||
} | ||
|
||
let mut right_max = 0; | ||
|
||
for (i, ((target, &top), &value)) in (0..).zip(temp.iter_mut().zip(&*cache).zip(&row).rev()) { | ||
right_max = right_max.max(top + i); | ||
*target = (*target).max(right_max - i) + u64::from(value as u32); | ||
} | ||
|
||
mem::swap(&mut cache, &mut temp); | ||
} | ||
|
||
cache.iter().copied().max().unwrap() as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_points(points: Vec<Vec<i32>>) -> i64 { | ||
Self::max_points(points) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_1937_maximum_number_of_points_with_cost/mod.rs
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,22 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn max_points(points: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities::Matrix; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[1, 2, 3], [1, 5, 1], [3, 1, 1]] as &dyn Matrix<_>, 9), | ||
(&[[1, 5], [2, 3], [4, 2]], 11), | ||
]; | ||
|
||
for (points, expected) in test_cases { | ||
assert_eq!(S::max_points(points.to_vec()), expected); | ||
} | ||
} | ||
} |