Maximum Earnings From Taxi - Complete Solution Guide
Maximum Earnings From Taxi is LeetCode problem 2008, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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] = [start i , end i , tip i ] denotes the i th passenger requesting a ride from point start i to point end i who is willing to give a tip i doll
Detailed Explanation
The problem asks us to find the maximum earnings a taxi driver can make by picking up passengers on a road with 'n' points, numbered from 1 to n. Each passenger ride is defined by a start point, an end point, and a tip. The driver can only carry one passenger at a time. The earnings for a ride are calculated as (end - start + tip). The objective is to maximize the total earnings by optimally selecting which passengers to pick up.
Solution Approach
The solution utilizes dynamic programming to determine the maximum earnings at each point 'i' on the road. A DP array 'dp' of size (n+1) is used, where dp[i] represents the maximum earnings possible up to point 'i'. The algorithm iterates from 1 to n. At each point 'i', it considers two possibilities: either not picking up any new ride and simply carrying over the maximum earnings from the previous point (dp[i-1]), or picking up a ride that ends at point 'i'. If a ride ends at point 'i', we calculate the potential earnings from that ride (end - start + tip) and add it to the maximum earnings achievable at the ride's starting point (dp[start]). We then update dp[i] to be the maximum of these two possibilities.
Step-by-Step Algorithm
- Step 1: Initialize a dynamic programming array `dp` of size `n+1` with all values set to 0. `dp[i]` represents the maximum earnings up to point `i`.
- Step 2: Create a list of lists `rides_by_end` to group rides by their end points. This allows us to efficiently access all rides ending at a particular point.
- Step 3: Iterate through the `rides` array and add each ride to the appropriate list in `rides_by_end` based on its end point.
- Step 4: Iterate from `i = 1` to `n`. For each point `i`, calculate the maximum earnings:
- Step 5: Option 1: `dp[i] = dp[i-1]` (carry over the maximum earnings from the previous point).
- Step 6: Option 2: Iterate through all rides ending at point `i`. For each such ride, calculate the profit (`i - start + tip`). Update `dp[i]` to be the maximum of its current value and `dp[start] + profit`.
- Step 7: After the loop finishes, `dp[n]` will contain the maximum earnings achievable up to point `n`, which is the final result.
Key Insights
- Insight 1: Dynamic programming is suitable because the problem has optimal substructure; the maximum earnings up to a point 'i' can be derived from the maximum earnings at previous points.
- Insight 2: Sorting rides directly is not necessary, but grouping rides ending at the same point simplifies the dynamic programming step.
- Insight 3: The core idea is to consider each point 'i' on the road and decide whether to extend the earnings from the previous point or to pick up a ride that ends at point 'i'. Choosing the maximum of these two options leads to the optimal solution.
Complexity Analysis
Time Complexity: O(n + m)
Space Complexity: O(n + m)
Topics
This problem involves: Array, Hash Table, Binary Search, Dynamic Programming, Sorting.
Companies
Asked at: Myntra.