2008. Maximum Earnings From Taxi
SourceBiweekly Contest 61 Q3DifficultyMediumRating1871
Description
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.
Example 1:
Input: n = 5, rides = [[2,5,4],[1,5,1]] Output: 7 Explanation: We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
Example 2:
Input: n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]] Output: 20 Explanation: We will pick up the following passengers: - Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars. - Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars. - Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars. We earn 9 + 5 + 6 = 20 dollars in total.
Constraints:
1 <= n <= 1051 <= rides.length <= 3 * 104rides[i].length == 31 <= starti < endi <= n1 <= tipi <= 105
Solutions
Solution 1: Memoization Search + Binary Search
Thinking
After sorting rides by start, ride \(i\) only combines with later rides whose start is \(\ge end_i\). Subsets are infeasible for \(m \le 3 \times 10^4\); the state is the best profit from index \(i\).
Skip goes to \(i+1\); take binary-searches the first start \(\ge end_i\) as \(j\) and adds distance, tip, and \(dfs(j)\).
Memoization evaluates each \(i\) once in \(O(m \log m)\).
First, we sort \(rides\) in ascending order by \(start\). Then we design a function \(dfs(i)\), which represents the maximum tip that can be obtained from accepting orders starting from the \(i\)-th passenger. The answer is \(dfs(0)\).
The calculation process of the function \(dfs(i)\) is as follows:
For the \(i\)-th passenger, we can choose to accept or not to accept the order. If we don't accept the order, the maximum tip that can be obtained is \(dfs(i + 1)\). If we accept the order, we can use binary search to find the first passenger encountered after the drop-off point of the \(i\)-th passenger, denoted as \(j\). The maximum tip that can be obtained is \(dfs(j) + end_i - start_i + tip_i\). Take the larger of the two. That is:
Where \(j\) is the smallest index that satisfies \(start_j \ge end_i\), which can be obtained by binary search.
In this process, we can use memoization search to save the answer of each state to avoid repeated calculations.
The time complexity is \(O(m \times \log m)\), and the space complexity is \(O(m)\). Here, \(m\) is the length of \(rides\).
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
Solution 2: Dynamic Programming + Binary Search
Thinking
Solution 1 is already \(O(m \log m)\), yet recursion and @cache add constants. Sorting by end lets \(f[i]\) be the best profit among the first \(i\) rides, with no aftereffect.
Skip is \(f[i-1]\); take binary-searches the last end \(\le start_i\). Filling \(f\) iteratively removes the call stack.
We can change the memoization search in Solution 1 to dynamic programming.
First, sort \(rides\), this time we sort by \(end\) in ascending order. Then define \(f[i]\), which represents the maximum tip that can be obtained from the first \(i\) passengers. Initially, \(f[0] = 0\), and the answer is \(f[m]\).
For the \(i\)-th passenger, we can choose to accept or not to accept the order. If we don't accept the order, the maximum tip that can be obtained is \(f[i-1]\). If we accept the order, we can use binary search to find the last passenger whose drop-off point is not greater than \(start_i\) before the \(i\)-th passenger gets on the car, denoted as \(j\). The maximum tip that can be obtained is \(f[j] + end_i - start_i + tip_i\). Take the larger of the two. That is:
Where \(j\) is the largest index that satisfies \(end_j \le start_i\), which can be obtained by binary search.
The time complexity is \(O(m \times \log m)\), and the space complexity is \(O(m)\). Here, \(m\) is the length of \(rides\).
Similar problems:
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |