-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaximum-earnings-from-taxi.cpp
44 lines (39 loc) · 1.25 KB
/
maximum-earnings-from-taxi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
using ll = long long;
long long maxTaxiEarnings(int n, vector<vector<int>>& rides) {
vector<ll> dp(n+1);
sort(rides.begin(), rides.end(), [](const vector<int> &a, const vector<int> &b){
return a[1] < b[1];
});
int j = 0;
for (int i = 1; i <= n; i++) {
dp[i] = dp[i-1];
// j = i;
while(j < rides.size() && rides[j][1] == i) {
dp[i] = max(dp[i], dp[rides[j][0]] + i - rides[j][0] + rides[j][2]);
j++;
}
}
return dp[n];
}
};
// class Solution {
// public:
// using ll = long long;
// long long maxTaxiEarnings(int n, vector<vector<int>>& rides) {
// vector<ll> dp(n+1, 0);
// sort(rides.begin(), rides.end(), [](const vector<int> &a, const vector<int> &b){
// return a[1] < b[1];
// });
// int j = 0;
// for (int i = 1; i <= n; ++i) {
// dp[i] = dp[i-1];
// while (j < rides.size() && rides[j][1] == i) {
// dp[i] = max(dp[i], dp[rides[j][0]]+i-rides[j][0]+rides[j][2]);
// ++j;
// }
// }
// return dp[n];
// }
// };