Skip to content

Commit 2d7386f

Browse files
committed
2008. Maximum Earnings From Taxi: RETRY
1 parent d0493cc commit 2d7386f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,3 +1513,4 @@ mod s2002_maximum_product_of_the_length_of_two_palindromic_subsequences;
15131513
mod s2003_smallest_missing_genetic_value_in_each_subtree;
15141514
mod s2006_count_number_of_pairs_with_absolute_difference_k;
15151515
mod s2007_find_original_array_from_doubled_array;
1516+
mod s2008_maximum_earnings_from_taxi;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* [2008] Maximum Earnings From Taxi
3+
*
4+
* There are n points on a road you are driving your taxi on. The n points on the road are labeled from 1 to n in the direction you are going, and you want to drive from point 1 to point n to make money by picking up passengers. You cannot change the direction of the taxi.
5+
* The passengers are represented by a 0-indexed 2D integer array rides, where rides[i] = [starti, endi, tipi] denotes the i^th passenger requesting a ride from point starti to point endi who is willing to give a tipi dollar tip.
6+
* For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive at most one passenger at a time.
7+
* Given n and rides, return the maximum number of dollars you can earn by picking up the passengers optimally.
8+
* Note: You may drop off a passenger and pick up a different passenger at the same point.
9+
*
10+
* Example 1:
11+
*
12+
* Input: n = 5, rides = [<u>[2,5,4]</u>,[1,5,1]]
13+
* Output: 7
14+
* Explanation: We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
15+
*
16+
* Example 2:
17+
*
18+
* Input: n = 20, rides = [[1,6,1],<u>[3,10,2]</u>,<u>[10,12,3]</u>,[11,12,2],[12,15,2],<u>[13,18,1]</u>]
19+
* Output: 20
20+
* Explanation: We will pick up the following passengers:
21+
* - Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
22+
* - Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
23+
* - Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
24+
* We earn 9 + 5 + 6 = 20 dollars in total.
25+
*
26+
* Constraints:
27+
*
28+
* 1 <= n <= 10^5
29+
* 1 <= rides.length <= 3 * 10^4
30+
* rides[i].length == 3
31+
* 1 <= starti < endi <= n
32+
* 1 <= tipi <= 10^5
33+
*
34+
*/
35+
pub struct Solution {}
36+
37+
// problem: https://leetcode.com/problems/maximum-earnings-from-taxi/
38+
// discuss: https://leetcode.com/problems/maximum-earnings-from-taxi/discuss/?currentPage=1&orderBy=most_votes&query=
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
pub fn max_taxi_earnings(n: i32, rides: Vec<Vec<i32>>) -> i64 {
44+
0
45+
}
46+
}
47+
48+
// submission codes end
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::*;
53+
54+
#[test]
55+
#[ignore]
56+
fn test_2008_example_1() {
57+
let n = 5;
58+
let rides = vec![vec![2, 5, 4], vec![1, 5, 1]];
59+
60+
let result = 7;
61+
62+
assert_eq!(Solution::max_taxi_earnings(n, rides), result);
63+
}
64+
65+
#[test]
66+
#[ignore]
67+
fn test_2008_example_2() {
68+
let n = 20;
69+
let rides = vec![
70+
vec![1, 6, 1],
71+
vec![3, 10, 2],
72+
vec![10, 12, 3],
73+
vec![11, 12, 2],
74+
vec![12, 15, 2],
75+
vec![13, 18, 1],
76+
];
77+
78+
let result = 20;
79+
80+
assert_eq!(Solution::max_taxi_earnings(n, rides), result);
81+
}
82+
}

0 commit comments

Comments
 (0)