Skip to content

Commit 30b9c99

Browse files
committed
Time: 536 ms (25.34%), Space: 6.5 MB (76.26%) - LeetHub
1 parent 4d49c6a commit 30b9c99

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

0132-palindrome-partitioning-ii/0132-palindrome-partitioning-ii.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,26 @@ class Solution {
3636

3737
int n = s.size();
3838

39-
vector<int> dp(n+1,-1);
39+
// vector<int> dp(n+1,-1);
4040

41-
return helper(0,s,dp) - 1;
42-
41+
// return helper(0,s,dp) - 1;
42+
43+
vector<int> dp(n+1,0);
44+
45+
for(int idx = n-1; idx>=0 ; --idx)
46+
{
47+
int ans = INT_MAX;
48+
for(int i =idx ; i < n; ++i)
49+
{
50+
if(isPalindrome(idx, i, s))
51+
{
52+
int cost = 1 + dp[i+1];
53+
ans = min(ans,cost);
54+
}
55+
}
56+
dp[idx] = ans;
57+
}
58+
59+
return dp[0] - 1;
4360
}
4461
};

0 commit comments

Comments
 (0)