diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 33f2902..e6e7f44 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -572,3 +572,5 @@ mod p2360_longest_cycle_in_a_graph; mod p2109_adding_spaces_to_a_string; mod p2278_percentage_of_letter_in_string; + +mod p2140_solving_questions_with_brainpower; diff --git a/src/problem/p2140_solving_questions_with_brainpower.rs b/src/problem/p2140_solving_questions_with_brainpower.rs new file mode 100644 index 0000000..b5d5570 --- /dev/null +++ b/src/problem/p2140_solving_questions_with_brainpower.rs @@ -0,0 +1,56 @@ +/** + * [2140] Solving Questions With Brainpower + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn most_points(questions: Vec>) -> i64 { + let questions: Vec<(i64, usize)> = questions + .into_iter() + .map(|a| (a[0] as i64, a[1] as usize)) + .collect(); + let n = questions.len(); + let mut dp = vec![0; n]; + + for i in (0..n).rev() { + let (score, brain_power) = questions[i]; + dp[i] = if i == n - 1 { 0 } else { dp[i + 1] }; + + // 选择做i题就需要跳过brian_power道题 + if i + brain_power + 1 < n { + dp[i] = dp[i].max(dp[i + brain_power + 1] + score); + } else { + dp[i] = dp[i].max(score); + } + } + + dp[0] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2140() { + assert_eq!( + 5, + Solution::most_points(vec![vec![3, 2], vec![4, 3], vec![4, 4], vec![2, 5]]) + ); + assert_eq!( + 7, + Solution::most_points(vec![ + vec![1, 1], + vec![2, 2], + vec![3, 3], + vec![4, 4], + vec![5, 5] + ]) + ); + } +}