Skip to content

Commit

Permalink
add: 出租车的最大盈利
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 8, 2023
1 parent 2ef7d19 commit b5f71a9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 排序

- [出租车的最大盈利](src/sort/maximum_earnings_from_taxi.rs) [数组, 二分查找, 动态规划, 排序]

- LeetCode 2008. 出租车的最大盈利 <https://leetcode.cn/problems/maximum-earnings-from-taxi>

- [可被三整除的最大和](src/sort/greatest_sum_divisible_by_three.rs) [贪心, 数组, 动态规划, 排序]

- LeetCode 1262. 可被三整除的最大和 <https://leetcode.cn/problems/greatest-sum-divisible-by-three>
Expand Down
Binary file added images/sort/maximum_earnings_from_taxi.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/sort/maximum_earnings_from_taxi.rs
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
}
}
1 change: 1 addition & 0 deletions src/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pub mod number_of_distinct_averages;
pub mod mice_and_cheese;
pub mod compare_strings_by_frequency_of_the_smallest_character;
pub mod greatest_sum_divisible_by_three;
pub mod maximum_earnings_from_taxi;
31 changes: 31 additions & 0 deletions tests/sort/maximum_earnings_from_taxi_test.rs
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);
}
1 change: 1 addition & 0 deletions tests/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pub mod number_of_distinct_averages_test;
pub mod mice_and_cheese_test;
pub mod compare_strings_by_frequency_of_the_smallest_character_test;
pub mod greatest_sum_divisible_by_three_test;
pub mod maximum_earnings_from_taxi_test;

0 comments on commit b5f71a9

Please sign in to comment.