20241001 finished.
This commit is contained in:
parent
022b39ec43
commit
b2e5f9ce9a
|
@ -250,4 +250,5 @@ mod p2535_difference_between_element_sum_and_digit_sum_of_an_array;
|
|||
mod p2516_take_k_of_each_character_from_left_and_right;
|
||||
mod p2286_booking_concert_tickets_in_groups;
|
||||
mod p2073_time_needed_to_buy_tickets;
|
||||
mod p1845_seat_reservation_manager;
|
||||
mod p1845_seat_reservation_manager;
|
||||
mod p983_minimum_cost_for_tickets;
|
51
src/problem/p983_minimum_cost_for_tickets.rs
Normal file
51
src/problem/p983_minimum_cost_for_tickets.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* [983] Minimum Cost For Tickets
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn mincost_tickets(days: Vec<i32>, costs: Vec<i32>) -> i32 {
|
||||
let mut memory = vec![None; 366];
|
||||
let days: HashSet<usize> = days.iter().map(|x| *x as usize).collect();
|
||||
|
||||
Self::dp(1, &mut memory, &days, &costs)
|
||||
}
|
||||
|
||||
fn dp(day: usize, memory: &mut Vec<Option<i32>>, days: &HashSet<usize>, costs: &Vec<i32>) -> i32 {
|
||||
if day > 365 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if let Some(result) = memory[day] {
|
||||
return result;
|
||||
}
|
||||
|
||||
let result = if days.contains(&day) {
|
||||
(Self::dp(day + 1, memory, days, costs) + costs[0])
|
||||
.min(Self::dp(day + 7, memory, days, costs) + costs[1])
|
||||
.min(Self::dp(day + 30, memory, days, costs) + costs[2])
|
||||
} else {
|
||||
Self::dp(day + 1, memory, days, costs)
|
||||
};
|
||||
|
||||
memory[day] = Some(result);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_983() {
|
||||
assert_eq!(11, Solution::mincost_tickets(vec![1, 4, 6, 7, 8, 20], vec![2, 7, 15]));
|
||||
assert_eq!(17, Solution::mincost_tickets(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], vec![2, 7, 15]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user