forked from deliteser112/user-app-react-node-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProfitK.js
More file actions
39 lines (33 loc) · 1.01 KB
/
maxProfitK.js
File metadata and controls
39 lines (33 loc) · 1.01 KB
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
function maxProfitK(prices, k) {
const n = prices.length;
if (n <= 1 || k === 0) {
return 0;
}
const dp = Array.from({ length: k + 1 }, () => Array(n).fill(0));
for (let i = 1; i <= k; i++) {
let maxDiff = -prices[0];
for (let j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j - 1], prices[j] + maxDiff);
maxDiff = Math.max(maxDiff, dp[i - 1][j] - prices[j]);
}
}
return dp[k][n - 1];
}
function runTests() {
const testCases = [
{ prices: [7, 1, 5, 3, 6, 4], k: 2 },
{ prices: [1, 2, 3, 4, 5], k: 2 },
{ prices: [7, 6, 4, 3, 1], k: 1 },
{ prices: [3, 2, 6, 5, 0, 3], k: 2 },
{ prices: [3, 3, 5, 0, 0, 3, 1, 4], k: 2 }
];
testCases.forEach((testCase, index) => {
const { prices, k } = testCase;
const result = maxProfitK(prices, k);
console.log(`Test Case ${index + 1}:`);
console.log("Input:", prices, k);
console.log("Output:", result);
console.log("----------------------");
});
}
runTests();