Skip to content

Commit 05e8682

Browse files
authored
Create 1547-minimum-cost-to-cut-a-stick.js
1 parent 41782ab commit 05e8682

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[]} cuts
4+
* @return {number}
5+
*/
6+
const minCost = function(n, cuts) {
7+
const x = 100 + 2
8+
const dp = Array.from({ length: x }, () => Array(x).fill(0))
9+
cuts.push(0, n)
10+
cuts.sort((a, b) => a - b)
11+
const res = dfs(0, cuts.length - 1)
12+
return res
13+
function dfs(i, j) {
14+
if(j - i <= 1) return 0
15+
if(!dp[i][j]) {
16+
dp[i][j] = Number.MAX_VALUE
17+
for(let k = i + 1; k < j; k++) {
18+
dp[i][j] = Math.min(dp[i][j], cuts[j] - cuts[i] + dfs(i, k) + dfs(k, j))
19+
}
20+
}
21+
return dp[i][j]
22+
}
23+
};

0 commit comments

Comments
 (0)