Maximum Earnings From Taxi - Problem

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.

The passengers are represented by a 0-indexed 2D integer array rides, where rides[i] = [starti, endi, tipi] denotes the ith passenger requesting a ride from point starti to point endi who is willing to give a tipi dollar tip.

For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive at most one passenger at a time.

Given n and rides, return the maximum number of dollars you can earn by picking up the passengers optimally.

Note: You may drop off a passenger and pick up a different passenger at the same point.

Input & Output

Example 1 — Basic Multiple Rides
$ Input: n = 5, rides = [[2,5,4],[1,5,1]]
Output: 7
💡 Note: Pick up the passenger from point 2 to point 5 with tip 4. Earnings = 5-2+4 = 7. The other ride overlaps so we can't take both.
Example 2 — Non-overlapping Rides
$ Input: n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]]
Output: 20
💡 Note: Take rides [1,6,1], [10,12,3], and [12,15,2] for earnings 6+5+5=16, or find better combination [3,10,2] + [12,15,2] + [13,18,1] = 9+5+6=20
Example 3 — Single Ride
$ Input: n = 3, rides = [[1,3,0]]
Output: 2
💡 Note: Only one ride available from point 1 to 3 with no tip. Earnings = 3-1+0 = 2

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ rides.length ≤ 3 × 104
  • rides[i].length == 3
  • 1 ≤ starti < endi ≤ n
  • 1 ≤ tipi ≤ 105

Visualization

Tap to expand
Maximum Earnings From Taxi INPUT Road (n = 5 points) 1 2 3 4 5 Rides Array rides[0] = [2, 5, 4] start:2, end:5, tip:4 Earn: 5-2+4 = $7 rides[1] = [1, 5, 1] start:1, end:5, tip:1 Earn: 5-1+1 = $5 Input Values n = 5 rides = [[2,5,4], [1,5,1]] ALGORITHM (DP) 1 Sort by end point Group rides ending at same point 2 Initialize DP array dp[i] = max earnings at point i 3 Transition formula dp[i] = max(dp[i-1], best ride) 4 Return dp[n] Maximum earnings at end DP Table i 1 2 3 4 dp[i] 0 0 0 0 i=5 dp[5] = max(5, 7) = 7 Ride[0]: dp[2] + 7 = 0 + 7 = 7 Ride[1]: dp[1] + 5 = 0 + 5 = 5 Best at point 5 = $7 FINAL RESULT Optimal Choice 1 2 3 4 5 Ride [2,5,4] Earnings Breakdown Distance: 5 - 2 = 3 Tip: 4 Total: 3 + 4 = $7 Output: 7 Maximum Earnings = $7 Key Insight: Use Dynamic Programming where dp[i] represents max earnings reaching point i. For each point, choose between: (1) not picking up any passenger ending here, or (2) picking the best ride ending here that gives dp[start] + (end - start + tip). Time: O(n + m), Space: O(n) TutorialsPoint - Maximum Earnings From Taxi | DP Approach
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
35.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen