[Leetcode] Palindrome Partitioning II
2014-11-01 04:29
429 查看
题目:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
Return
using 1 cut.
思路一 (TLE):延续Palindrome Partitioning I的思路,在DP的基础上再套用一层DP,从每个字符开始扩展。然后做一次递归,遍历所有可能,更新最小值。这样做会超时,需要寻找新的方法。
总结:复杂度为O(2^n).
思路二:思路一用了两次DP依然超时,那么唯一可以改进的地方就是递归的部分了。思路一的做法是寻找所有可能的分割,然后找最小值。那么想到,在某一个点的分割值可能会被多次用到。例如,0可能跳到5这个点,0也可能先跳到2再跳到5,那么5的结果就被用到了2次。于是考虑到,再用一次DP解决问题。对于一个点i来说,首先可能的分割是这个点前面的点的分割数 + 1. 也就是,0 ... i - 1 | i 这样分割。但如果i的前面存在一个点j,使得
j ... i 构成了回文。那么一种新的分割方式为 0 ... j - 1 | j ... i. 这样,遍历i前面的所有点,即可以找到i的最小分割。利用一个数组存i的结果,在i后面的点可能会多次用到这个结果,这就是DP的思想。
总结:计算record数组的值以及后面的DP复杂度都为O(n^2),因此最后的复杂度为O(n^2). 可以看到采用DP后,计算最小分割没有增加复杂度。
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
"aab",
Return
1since the palindrome partitioning
["aa","b"]could be produced
using 1 cut.
思路一 (TLE):延续Palindrome Partitioning I的思路,在DP的基础上再套用一层DP,从每个字符开始扩展。然后做一次递归,遍历所有可能,更新最小值。这样做会超时,需要寻找新的方法。
class Solution { public: int cut; void min_cut_helper(const vector<vector<bool>>& record, const string& s, int pos, int cut, int& min_cut) { if (pos == (int)s.size()) { min_cut = min_cut < cut ? min_cut : cut; return; } for (int i = pos; i < (int)s.size(); ++i) { if (record[pos][i]) { min_cut_helper(record, s, i + 1, cut + 1, min_cut); } } } int minCut(string s) { vector<vector<bool>> record(s.size(), vector<bool>(s.size(), false)); for (int i = 0; i < (int)s.size(); ++i) { record[i][i] = true; // odd length for (int j = 1; i - j >= 0 && i + j < (int)s.size(); ++j) { if (s[i-j] == s[i+j]) record[i-j][i+j] = true; else break; } // even length for (int j = 0; i - j >= 0 && i + 1 + j < (int)s.size(); ++j) { if (s[i-j] == s[i+1+j]) record[i-j][i+1+j] = true; else break; } } int min_cut = INT_MAX; min_cut_helper(record, s, 0, 0, min_cut); return min_cut; } };
总结:复杂度为O(2^n).
思路二:思路一用了两次DP依然超时,那么唯一可以改进的地方就是递归的部分了。思路一的做法是寻找所有可能的分割,然后找最小值。那么想到,在某一个点的分割值可能会被多次用到。例如,0可能跳到5这个点,0也可能先跳到2再跳到5,那么5的结果就被用到了2次。于是考虑到,再用一次DP解决问题。对于一个点i来说,首先可能的分割是这个点前面的点的分割数 + 1. 也就是,0 ... i - 1 | i 这样分割。但如果i的前面存在一个点j,使得
j ... i 构成了回文。那么一种新的分割方式为 0 ... j - 1 | j ... i. 这样,遍历i前面的所有点,即可以找到i的最小分割。利用一个数组存i的结果,在i后面的点可能会多次用到这个结果,这就是DP的思想。
class Solution { public: int minCut(string s) { if (s.size() <= 0) return -1; vector<vector<bool>> record(s.size(), vector<bool>(s.size(), false)); for (int i = 0; i < (int)s.size(); ++i) { record[i][i] = true; // odd length for (int j = 1; i - j >= 0 && i + j < (int)s.size(); ++j) { if (s[i-j] == s[i+j]) record[i-j][i+j] = true; else break; } // even length for (int j = 0; i - j >= 0 && i + 1 + j < (int)s.size(); ++j) { if (s[i-j] == s[i+1+j]) record[i-j][i+1+j] = true; else break; } } vector<int> cut(s.size(), 0); cut[0] = 0; for (int i = 1; i < (int)cut.size(); ++i) { if (record[0][i]) { cut[i] = 0; continue; } cut[i] = cut[i-1] + 1; for (int j = 1; j < i; ++j) { if (record[j][i]) { cut[i] = cut[i] < (cut[j-1] + 1) ? cut[i] : (cut[j-1] + 1); } } } return cut.back(); } };
总结:计算record数组的值以及后面的DP复杂度都为O(n^2),因此最后的复杂度为O(n^2). 可以看到采用DP后,计算最小分割没有增加复杂度。
相关文章推荐
- [leetcode] Palindrome Partitioning II
- Palindrome Partitioning II Leetcode Python
- [LeetCode] Palindrome Partitioning II
- LeetCode : Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Palindrome Partitioning II Leetcode java
- Palindrome Partitioning II Leetcode
- leetcode之 Palindrome Partitioning I&II
- Leetcode: Palindrome Partitioning I & II
- LeetCode:Palindrome Partitioning II
- LeetCode132 Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Leetcode--Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- Leetcode Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- LeetCode Palindrome Partitioning II
- [LeetCode 132] - 回文分割II(Palindrome Partitioning II)