20241025 finished.
This commit is contained in:
parent
bce8de1c85
commit
8d364ed373
|
@ -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;
|
||||
|
|
48
src/problem/p3180_maximum_total_reward_using_operations_i.rs
Normal file
48
src/problem/p3180_maximum_total_reward_using_operations_i.rs
Normal file
|
@ -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>) -> i32 {
|
||||
let mut reward_values: Vec<usize> = 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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user