-
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 2140: Solving Questions With Brainpower
- Loading branch information
Showing
3 changed files
with
61 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
39 changes: 39 additions & 0 deletions
39
src/problem_2140_solving_questions_with_brainpower/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,39 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
pub fn most_points(questions: Vec<Vec<i32>>) -> i64 { | ||
let mut cache = vec![0; questions.len()].into_boxed_slice(); | ||
|
||
for (i, question) in questions.into_iter().enumerate().rev() { | ||
let [points, brainpower]: [_; 2] = question.try_into().ok().unwrap(); | ||
let score_if_skip = cache.get(i + 1).copied().unwrap_or(0); | ||
|
||
let score_if_solve = | ||
u64::from(points as u32) + cache.get(i + 1 + brainpower as u32 as usize).copied().unwrap_or(0); | ||
|
||
cache[i] = score_if_skip.max(score_if_solve); | ||
} | ||
|
||
cache[0] as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn most_points(questions: Vec<Vec<i32>>) -> i64 { | ||
Self::most_points(questions) | ||
} | ||
} | ||
|
||
#[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,21 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn most_points(questions: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[3, 2], [4, 3], [4, 4], [2, 5]] as &[_], 5), | ||
(&[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], 7), | ||
]; | ||
|
||
for (questions, expected) in test_cases { | ||
assert_eq!(S::most_points(questions.iter().map(Vec::from).collect()), expected); | ||
} | ||
} | ||
} |