-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 出租车的最大盈利 | ||
// https://leetcode.cn/problems/maximum-earnings-from-taxi | ||
// INLINE ../../images/sort/maximum_earnings_from_taxi.jpeg | ||
|
||
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; | ||
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]); | ||
j += 1; | ||
} | ||
} | ||
dp[n as usize] as i64 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use rust_practice::sort::maximum_earnings_from_taxi::Solution; | ||
|
||
#[test] | ||
fn max_taxi_earnings() { | ||
// 示例 1: | ||
// 输入:n = 5, rides = [[2,5,4],[1,5,1]] | ||
// 输出:7 | ||
// 解释:我们可以接乘客 0 的订单,获得 5 - 2 + 4 = 7 元。 | ||
let n = 5; | ||
let rides = vec![vec![2, 5, 4], vec![1, 5, 1]]; | ||
assert_eq!(Solution::max_taxi_earnings(n, rides), 7); | ||
|
||
// 示例 2: | ||
// 输入:n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]] | ||
// 输出:20 | ||
// 解释:我们可以接以下乘客的订单: | ||
// - 将乘客 1 从地点 3 送往地点 10 ,获得 10 - 3 + 2 = 9 元。 | ||
// - 将乘客 2 从地点 10 送往地点 12 ,获得 12 - 10 + 3 = 5 元。 | ||
// - 将乘客 5 从地点 13 送往地点 18 ,获得 18 - 13 + 1 = 6 元。 | ||
// 我们总共获得 9 + 5 + 6 = 20 元。 | ||
let n = 20; | ||
let rides = vec![ | ||
vec![1, 6, 1], | ||
vec![3, 10, 2], | ||
vec![10, 12, 3], | ||
vec![11, 12, 2], | ||
vec![12, 15, 2], | ||
vec![13, 18, 1], | ||
]; | ||
assert_eq!(Solution::max_taxi_earnings(n, rides), 20); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters