Skip to content

Commit

Permalink
Time: 959 ms (70.88%), Space: 101.1 MB (75.11%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Nov 27, 2022
1 parent 6779d38 commit 3c3b61c
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:

int getIndex(vector<vector<int>> &rides,int l,int target,int n)
{
int r=n-1;
int res=n+1;
while(l<=r)
{
int mid=l+(r-l)/2;
if(rides[mid][0]>=target)
{
res=mid;
r=mid-1;
}
else
l=mid+1;
}
return res;
}

long long solve(vector<vector<int>> &rides,int i,int n,vector<long long> &dp)
{
if(i>=n)return 0;
if(dp[i]!=-1)return dp[i];
int ind=getIndex(rides,i+1,rides[i][1],n);
//take
long long incl=((rides[i][1]-rides[i][0])+rides[i][2])+ solve(rides,ind,n,dp);
//not take
long long excl=solve(rides,i+1,n,dp);
return dp[i]=max(incl,excl);

}
long long maxTaxiEarnings(int n, vector<vector<int>>& rides) {
n=rides.size();
sort(rides.begin(),rides.end());
vector<long long> dp(n,-1);
return solve(rides,0,n,dp);

}
};

0 comments on commit 3c3b61c

Please sign in to comment.