Skip to content

Commit 11a3f52

Browse files
authored
Create 2830-maximize-the-profit-as-the-salesman.js
1 parent 91640a0 commit 11a3f52

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} offers
4+
* @return {number}
5+
*/
6+
const maximizeTheProfit = function(n, offers) {
7+
const dp = Array(n + 1).fill(0);
8+
const m = [];
9+
for (const a of offers) {
10+
if(m[a[1]] == null) m[a[1]] = []
11+
m[a[1]].push(a);
12+
}
13+
for (let e = 1; e <= n; e++) {
14+
dp[e] = dp[e - 1];
15+
for (let a of (m[e - 1] || []) ) {
16+
dp[e] = Math.max(dp[e], dp[a[0]] + a[2]);
17+
}
18+
}
19+
return dp[n];
20+
};

0 commit comments

Comments
 (0)