Skip to content

Commit 703dabf

Browse files
Sync LeetCode submission Runtime - 75 ms (62.57%), Memory - 155 MB (22.63%)
1 parent e845d0c commit 703dabf

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>There are <code>n</code> points on a road you are driving your taxi on. The <code>n</code> points on the road are labeled from <code>1</code> to <code>n</code> in the direction you are going, and you want to drive from point <code>1</code> to point <code>n</code> to make money by picking up passengers. You cannot change the direction of the taxi.</p>
2+
3+
<p>The passengers are represented by a <strong>0-indexed</strong> 2D integer array <code>rides</code>, where <code>rides[i] = [start<sub>i</sub>, end<sub>i</sub>, tip<sub>i</sub>]</code> denotes the <code>i<sup>th</sup></code> passenger requesting a ride from point <code>start<sub>i</sub></code> to point <code>end<sub>i</sub></code> who is willing to give a <code>tip<sub>i</sub></code> dollar tip.</p>
4+
5+
<p>For<strong> each </strong>passenger <code>i</code> you pick up, you <strong>earn</strong> <code>end<sub>i</sub> - start<sub>i</sub> + tip<sub>i</sub></code> dollars. You may only drive <b>at most one </b>passenger at a time.</p>
6+
7+
<p>Given <code>n</code> and <code>rides</code>, return <em>the <strong>maximum</strong> number of dollars you can earn by picking up the passengers optimally.</em></p>
8+
9+
<p><strong>Note:</strong> You may drop off a passenger and pick up a different passenger at the same point.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> n = 5, rides = [<u>[2,5,4]</u>,[1,5,1]]
16+
<strong>Output:</strong> 7
17+
<strong>Explanation:</strong> We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> 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>]
24+
<strong>Output:</strong> 20
25+
<strong>Explanation:</strong> We will pick up the following passengers:
26+
- Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
27+
- Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
28+
- Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
29+
We earn 9 + 5 + 6 = 20 dollars in total.</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
36+
<li><code>1 &lt;= rides.length &lt;= 3 * 10<sup>4</sup></code></li>
37+
<li><code>rides[i].length == 3</code></li>
38+
<li><code>1 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= n</code></li>
39+
<li><code>1 &lt;= tip<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
40+
</ul>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
long long maxTaxiEarnings(int n, vector<vector<int>>& rides) {
4+
vector<vector<pair<int, int>>> endTimes(n+1);
5+
for (vector<int>& r : rides) endTimes[r[1]].push_back({r[0], r[2]});
6+
vector<long long> dp(n+1, 0);
7+
for (int i=2; i<=n; i++) {
8+
dp[i] = dp[i-1];
9+
for (auto& [start, tip] : endTimes[i]) {
10+
dp[i] = max(dp[i], i - start + tip + dp[start]);
11+
}
12+
}
13+
return dp[n];
14+
}
15+
};

0 commit comments

Comments
 (0)