Maximum Earnings From Taxi - Problem

You're a taxi driver on a straight road with n numbered points (1 to n). Your goal is to drive from point 1 to point n while maximizing your earnings by strategically picking up passengers.

Each passenger is represented as [start, end, tip] where:

  • start: pickup point
  • end: dropoff point
  • tip: additional tip in dollars

For each ride, you earn: (end - start) + tip dollars

Key Rules:

  • ๐Ÿš— You can only carry one passenger at a time
  • ๐Ÿšซ You cannot change direction (always move forward)
  • โœ… You can drop off and pick up different passengers at the same point

Return the maximum dollars you can earn by optimally selecting passengers.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 5, rides = [[2,5,4],[1,5,1],[4,6,2]]
โ€บ Output: 7
๐Ÿ’ก Note: Pick up passenger 2 who tips 4 dollars. Earnings = 5-2+4 = 7. This is better than taking passenger 1 (earnings = 5-1+1 = 5) or passenger 3 (earnings = 6-4+2 = 4).
example_2.py โ€” Multiple Non-overlapping
$ 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]. Total earnings: (6-1+1) + (12-10+3) + (15-12+2) = 6 + 5 + 5 = 16. Wait, let me recalculate: optimal is actually taking [3,10,2] + [12,15,2] + [13,18,1] = 9 + 5 + 6 = 20.
example_3.py โ€” Single Ride
$ Input: n = 10, rides = [[5,8,3]]
โ€บ Output: 6
๐Ÿ’ก Note: Only one ride available. Earnings = 8-5+3 = 6 dollars.

Visualization

Tap to expand
๐Ÿš• Maximum Taxi Earnings StrategyInput Rides:[2,5,4]Earn: 5-2+4 = 7[1,5,1]Earn: 5-1+1 = 5[4,6,2]Earn: 6-4+2 = 4Timeline Visualization:12345[1,5,1] โ†’ $5[2,5,4] โ†’ $7[4,6,2] โ†’ $4Solution Process:1. Sort by end time2. DP: take or skip?3. Binary searchOptimal Result:Maximum Earnings: $7Take ride [2,5,4] onlyTime: O(m log m) | Space: O(m)
Understanding the Visualization
1
Sort by End Times
Sort all rides by their end times to process them in optimal order
2
Dynamic Programming
For each ride, decide: take it (plus best earnings before it started) or skip it
3
Binary Search
Quickly find the latest ride that doesn't overlap with the current one
4
Build Optimal Solution
Incrementally build the maximum earnings possible
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic interval scheduling optimization problem. Sort intervals by end time, then use DP with binary search to efficiently find the best combination of non-overlapping rides.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^m)

We try all 2^m possible combinations of m rides

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Recursion stack depth and storage for current combination

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค rides.length โ‰ค 3 ร— 104
  • rides[i].length == 3
  • 1 โ‰ค starti < endi โ‰ค n
  • 1 โ‰ค tipi โ‰ค 105
  • All start and end points are integers
Asked in
Google 15 Amazon 12 Uber 25 Lyft 18
23.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