Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 07de3a7

Browse files
committed
Add solution #2008
1 parent 5c5158d commit 07de3a7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,684 LeetCode solutions in JavaScript
1+
# 1,685 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1540,6 +1540,7 @@
15401540
2003|[Smallest Missing Genetic Value in Each Subtree](./solutions/2003-smallest-missing-genetic-value-in-each-subtree.js)|Hard|
15411541
2006|[Count Number of Pairs With Absolute Difference K](./solutions/2006-count-number-of-pairs-with-absolute-difference-k.js)|Easy|
15421542
2007|[Find Original Array From Doubled Array](./solutions/2007-find-original-array-from-doubled-array.js)|Medium|
1543+
2008|[Maximum Earnings From Taxi](./solutions/2008-maximum-earnings-from-taxi.js)|Medium|
15431544
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15441545
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15451546
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 2008. Maximum Earnings From Taxi
3+
* https://leetcode.com/problems/maximum-earnings-from-taxi/
4+
* Difficulty: Medium
5+
*
6+
* There are n points on a road you are driving your taxi on. The n points on the road are
7+
* labeled from 1 to n in the direction you are going, and you want to drive from point 1
8+
* to point n to make money by picking up passengers. You cannot change the direction of the taxi.
9+
*
10+
* The passengers are represented by a 0-indexed 2D integer array rides, where
11+
* rides[i] = [starti, endi, tipi] denotes the ith passenger requesting a ride from point
12+
* starti to point endi who is willing to give a tipi dollar tip.
13+
*
14+
* For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive
15+
* at most one passenger at a time.
16+
*
17+
* Given n and rides, return the maximum number of dollars you can earn by picking up the
18+
* passengers optimally.
19+
*
20+
* Note: You may drop off a passenger and pick up a different passenger at the same point.
21+
*/
22+
23+
/**
24+
* @param {number} n
25+
* @param {number[][]} rides
26+
* @return {number}
27+
*/
28+
var maxTaxiEarnings = function(n, rides) {
29+
rides.sort((a, b) => a[1] - b[1]);
30+
const dp = new Array(n + 1).fill(0);
31+
let rideIndex = 0;
32+
33+
for (let point = 1; point <= n; point++) {
34+
dp[point] = dp[point - 1];
35+
while (rideIndex < rides.length && rides[rideIndex][1] === point) {
36+
const [start, end, tip] = rides[rideIndex];
37+
dp[point] = Math.max(dp[point], dp[start] + (end - start + tip));
38+
rideIndex++;
39+
}
40+
}
41+
42+
return dp[n];
43+
};

0 commit comments

Comments
 (0)