20250401 finished.
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
							
								
								
									
										56
									
								
								src/problem/p2140_solving_questions_with_brainpower.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/problem/p2140_solving_questions_with_brainpower.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| /** | ||||
|  * [2140] Solving Questions With Brainpower | ||||
|  */ | ||||
| pub struct Solution {} | ||||
|  | ||||
| // submission codes start here | ||||
|  | ||||
| impl Solution { | ||||
|     pub fn most_points(questions: Vec<Vec<i32>>) -> 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] | ||||
|             ]) | ||||
|         ); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user