Skip to content

Commit 0410f93

Browse files
committed
Runtime: 193 ms (Top 16.67%) | Memory: 45.6 MB (Top 100.00%)
1 parent 041f6d3 commit 0410f93

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// Runtime: 193 ms (Top 16.67%) | Memory: 45.6 MB (Top 100.00%)
2+
13
var palindromePartition = function(s, k) {
24
const len = s.length;
3-
5+
46
const cost = (i = 0, j = 0) => {
57
let c = 0;
68
while(i <= j) {
@@ -9,27 +11,27 @@ var palindromePartition = function(s, k) {
911
}
1012
return c;
1113
}
12-
14+
1315
const dp = Array.from({ length: len }, () => {
1416
return new Array(k + 1).fill(-1);
1517
})
16-
18+
1719
const splitHelper = (idx = 0, sl = k) => {
1820
if(sl < 0) return Infinity;
1921
if(idx == len) {
2022
if(sl == 0) return 0;
2123
return Infinity;
2224
}
23-
25+
2426
if(dp[idx][sl] != -1) return dp[idx][sl];
25-
27+
2628
let ans = Infinity;
27-
29+
2830
for(let i = idx; i < len; i++) {
2931
ans = Math.min(ans, splitHelper(i + 1, sl - 1) + cost(idx, i));
3032
}
3133
return dp[idx][sl] = ans;
3234
}
33-
35+
3436
return splitHelper();
35-
};
37+
};

0 commit comments

Comments
 (0)