Class Solution
- java.lang.Object
-
- g2001_2100.s2008_maximum_earnings_from_taxi.Solution
-
public class Solution extends Object
2008 - Maximum Earnings From Taxi.Medium
There are
npoints on a road you are driving your taxi on. Thenpoints on the road are labeled from1tonin the direction you are going, and you want to drive from point1to pointnto 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, whererides[i] = [starti, endi, tipi]denotes theithpassenger requesting a ride from pointstartito pointendiwho is willing to give atipidollar tip.For each passenger
iyou pick up, you earnendi - starti + tipidollars. You may only drive at most one passenger at a time.Given
nandrides, 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
-
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longmaxTaxiEarnings(int n, int[][] rides)
-