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
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
โ Linear Growth
Space Complexity
O(m)
Recursion stack depth and storage for current combination
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code