Skip to content

Commit

Permalink
fix: 出租车的最大盈利
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 8, 2023
1 parent b5f71a9 commit 913b7dc
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/sort/maximum_earnings_from_taxi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ pub struct Solution;

impl Solution {
pub fn max_taxi_earnings(n: i32, rides: Vec<Vec<i32>>) -> i64 {
let mut dp = vec![0; (n + 1) as usize];
let mut rides = rides;
let mut dp = vec![0i64; (n + 1) as usize];
let mut rides: Vec<Vec<i64>> = rides
.into_iter()
.map(|ride| ride.into_iter().map(|x| x as i64).collect())
.collect();

rides.sort_by(|a, b| a[1].cmp(&b[1]));
let mut j = 0;
for i in 1..=n {
dp[i as usize] = dp[(i - 1) as usize];
while j < rides.len() && rides[j][1] == i {
dp[i as usize] = dp[i as usize]
.max(dp[rides[j][0] as usize] + rides[j][1] - rides[j][0] + rides[j][2]);
let mut j = 0usize;
for i in 1..=n as usize {
dp[i] = dp[i - 1];
while j < rides.len() && rides[j][1] == i as i64 {
dp[i] =
dp[i].max(dp[rides[j][0] as usize] + rides[j][1] - rides[j][0] + rides[j][2]);
j += 1;
}
}
dp[n as usize] as i64
dp[n as usize]
}
}

0 comments on commit 913b7dc

Please sign in to comment.