Skip to content

Commit

Permalink
Update p1928
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Oct 3, 2024
1 parent 7c0ba7e commit 54e2c76
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ Call test by running `cargo test p[xxx]_`.
- [2861. Maximum Number of Alloys](src/leetcode/p2861_maximum_number_of_alloys)
- [2865. Beautiful Towers I](src/leetcode/p2865_beautiful_towers_i)

### Hard(62)
### Hard(63)

- [4. Median of Two Sorted Arrays](src/leetcode/p4_median_of_two_sorted_arrays)
- [10. Regular Expression Matching](src/leetcode/p10_regular_expression_matching)
Expand Down Expand Up @@ -347,6 +347,7 @@ Call test by running `cargo test p[xxx]_`.
- [1793. Maximum Score of a Good Subarray](src/leetcode/p1793_maximum_score_of_a_good_subarray)
- [1819. Number of Different Subsequences GCDs](src/leetcode/p1819_number_of_different_subsequences_gcds)
- [1825. Finding MK Average](src/leetcode/p1825_finding_mk_average)
- [1928. Minimum Cost to Reach Destination in Time](src/leetcode/p1928_minimum_cost_to_reach_destination_in_time)
- [1944. Number of Visible People in a Queue](src/leetcode/p1944_number_of_visible_people_in_a_queue)
- [2312. Selling Pieces of Wood](src/leetcode/p2312_selling_pieces_of_wood)
- [2376. Count Special Integers](src/leetcode/p2376_count_special_integers)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub mod p1870_minimum_speed_to_arrive_on_time;
pub mod p18_4sum;
pub mod p190_reverse_bits;
pub mod p191_number_of_1_bits;
pub mod p1928_minimum_cost_to_reach_destination_in_time;
pub mod p1944_number_of_visible_people_in_a_queue;
pub mod p1954_minimum_garden_perimeter_to_collect_enough_apples;
pub mod p1962_remove_stones_to_minimize_the_total;
Expand Down
101 changes: 101 additions & 0 deletions src/leetcode/p1928_minimum_cost_to_reach_destination_in_time/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
pub struct Solution;

impl Solution {
pub fn min_cost(max_time: i32, edges: Vec<Vec<i32>>, passing_fees: Vec<i32>) -> i32 {
let len = passing_fees.len();
let mut dp = vec![vec![i32::MAX; len]; max_time as usize + 1];
dp[0][0] = passing_fees[0];

for t in 1..max_time + 1 {
for edge in &edges {
let (i, j, cost) = (edge[0] as usize, edge[1] as usize, edge[2]);
if cost <= t {
if dp[(t - cost) as usize][j] != i32::MAX {
dp[t as usize][i] =
dp[t as usize][i].min(dp[(t - cost) as usize][j] + passing_fees[i]);
}
if dp[(t - cost) as usize][i] != i32::MAX {
dp[t as usize][j] =
dp[t as usize][j].min(dp[(t - cost) as usize][i] + passing_fees[j]);
}
}
}
}

let result = (1..max_time + 1)
.map(|t| dp[t as usize][len - 1])
.min()
.unwrap();
if result == i32::MAX {
-1
} else {
result
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::common::ToVecVec;

#[test]
fn test1() {
assert_eq!(
Solution::min_cost(
30,
[
[0, 1, 10],
[1, 2, 10],
[2, 5, 10],
[0, 3, 1],
[3, 4, 10],
[4, 5, 15]
]
.to_vec_vec(),
[5, 1, 2, 20, 20, 3].to_vec()
),
11
);
}

#[test]
fn test2() {
assert_eq!(
Solution::min_cost(
29,
[
[0, 1, 10],
[1, 2, 10],
[2, 5, 10],
[0, 3, 1],
[3, 4, 10],
[4, 5, 15]
]
.to_vec_vec(),
[5, 1, 2, 20, 20, 3].to_vec()
),
48
);
}

#[test]
fn test3() {
assert_eq!(
Solution::min_cost(
25,
[
[0, 1, 10],
[1, 2, 10],
[2, 5, 10],
[0, 3, 1],
[3, 4, 10],
[4, 5, 15]
]
.to_vec_vec(),
[5, 1, 2, 20, 20, 3].to_vec()
),
-1
);
}
}

0 comments on commit 54e2c76

Please sign in to comment.