Last active
November 29, 2017 14:43
-
-
Save songouyang/a3260cc9cab6451630e2390cbdd232f4 to your computer and use it in GitHub Desktop.
132. Palindrome Partitioning II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minCut(string s) { | |
unsigned long t = s.size(); | |
vector<vector<bool>> p(t, vector<bool>(t, false)); | |
vector<int> dp(t, INT_MAX); | |
for (int i = 0; i < t; ++i) { | |
for (int j = i; j >= 0; --j) { | |
if ((i - j <= 1 || p[j + 1][i - 1]) && s[i] == s[j]) { | |
p[j][i] = true; | |
} | |
if (p[j][i]) { | |
if (j == 0) { | |
dp[i] = 0; | |
} else { | |
dp[i] = min(dp[i], dp[j - 1] + 1); | |
} | |
} | |
} | |
} | |
return dp[t - 1]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment