Skip to content

Commit 20c8c4d

Browse files
authored
Update 1547-minimum-cost-to-cut-a-stick.js
1 parent a06b518 commit 20c8c4d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1547-minimum-cost-to-cut-a-stick.js

+24
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ const minCost = function(n, cuts) {
2121
return dp[i][j]
2222
}
2323
};
24+
25+
// another
26+
27+
/**
28+
* @param {number} n
29+
* @param {number[]} cuts
30+
* @return {number}
31+
*/
32+
const minCost = function (n, cuts) {
33+
cuts.push(0, n)
34+
cuts.sort((a, b) => a - b)
35+
const N = cuts.length,
36+
dp = Array.from({ length: N }, () => Array(N).fill(0))
37+
for (let l = 2; l < N; l++) {
38+
for (let i = 0; i + l < N; i++) {
39+
const j = i + l
40+
dp[i][j] = Infinity
41+
for (let k = i + 1; k < j; k++) {
42+
dp[i][j] = Math.min(dp[i][j], cuts[j] - cuts[i] + dp[i][k] + dp[k][j])
43+
}
44+
}
45+
}
46+
return dp[0][N - 1]
47+
}

0 commit comments

Comments
 (0)