diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 47422d8..978330d 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -274,3 +274,5 @@ mod p910_smallest_range_ii; mod p977_squares_of_a_sorted_array; mod p983_minimum_cost_for_tickets; mod p997_find_the_town_judge; + +mod p3180_maximum_total_reward_using_operations_i; diff --git a/src/problem/p3180_maximum_total_reward_using_operations_i.rs b/src/problem/p3180_maximum_total_reward_using_operations_i.rs new file mode 100644 index 0000000..f385c28 --- /dev/null +++ b/src/problem/p3180_maximum_total_reward_using_operations_i.rs @@ -0,0 +1,48 @@ +/** + * [3180] Maximum Total Reward Using Operations I + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn max_total_reward(reward_values: Vec) -> i32 { + let mut reward_values: Vec = reward_values.into_iter().map(|x| x as usize).collect(); + reward_values.sort(); + + let max_value = *reward_values.last().unwrap(); + let mut dp = vec![false; max_value * 2]; + dp[0] = true; + + for &x in reward_values.iter() { + for i in (x..=(2 * x - 1)).rev() { + if dp[i - x] { + dp[i] = true; + } + } + } + + let mut result = 0; + + for (i, &x) in dp.iter().enumerate() { + if x { + result = i; + } + } + + result as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3180() { + assert_eq!(4, Solution::max_total_reward(vec![1, 1, 3, 3])); + assert_eq!(11, Solution::max_total_reward(vec![1, 6, 4, 3, 2])); + } +}