Leetcode 2008: Maximum Earnings From Taxi

grid47
grid47
Exploring patterns and algorithms
Apr 20, 2024 8 min read

You are driving a taxi along a road with n points. Each point represents a location on the road. Passengers request rides from one point to another, and for each ride, you earn the distance traveled plus a tip. Your goal is to maximize your earnings by picking up passengers optimally. You can only carry one passenger at a time, and may pick up a new passenger at any point after dropping off the previous one.
Problem
Approach
Steps
Complexity
Input: The input consists of two values, `n` and `rides`. `n` is the number of points on the road (1 <= n <= 10^5), and `rides` is a list of ride requests. Each ride is represented as a triplet [start, end, tip], where start and end are points on the road (1 <= start < end <= n), and tip is the additional tip the passenger is willing to give (1 <= tip <= 10^5).
Example: n = 5, rides = [[2,5,4],[1,5,1]]
Constraints:
• 1 <= n <= 10^5
• 1 <= rides.length <= 3 * 10^4
• rides[i].length == 3
• 1 <= start < end <= n
• 1 <= tipi <= 10^5
Output: Return the maximum earnings you can achieve by picking up passengers optimally.
Example: 7
Constraints:
• The output is a single integer representing the maximum earnings.
Goal: Maximize the total earnings by picking up passengers in such a way that no two passengers' rides overlap in terms of the points they cover.
Steps:
• Sort the rides based on the start point of each ride.
• Use dynamic programming to determine the maximum earnings by evaluating each possible ride combination.
• For each ride, determine if it is optimal to pick it up or skip it by considering the maximum earnings from subsequent non-overlapping rides.
Goal: Ensure that the problem constraints are met for input size and values.
Steps:
• 1 <= n <= 10^5
• 1 <= rides.length <= 3 * 10^4
• rides[i].length == 3
• 1 <= start < end <= n
• 1 <= tipi <= 10^5
Assumptions:
• The ride requests are valid, and each request has valid start and end points.
Input: n = 5, rides = [[2,5,4],[1,5,1]]
Explanation: You have two ride requests. You can pick up passenger 0 from point 2 to 5 and earn `5 - 2 + 4 = 7` dollars. Thus, the maximum earnings is 7.

Input: n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]]
Explanation: By optimally selecting the rides, you can earn a total of 20 dollars from the given requests.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus