Skip to content

Commit a07b3ca

Browse files
committed
update 132
1 parent 4a68df5 commit a07b3ca

File tree

3 files changed

+88
-15
lines changed

3 files changed

+88
-15
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @Author: xuezaigds@gmail.com
3+
* @Last Modified time: 2016-10-11 10:06:28
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Dynamic Programming:
10+
cuts[i]: minimum cuts needed for a palindrome partitioning of s[i:], so we want cuts[0].
11+
12+
To get cuts[i-1], we scan j from i-1 to len(s)-1.
13+
Once we comes to a is_palindrome[i-1][j]==true:
14+
if j==len(s)-1, the string s[i-1:] is a Pal, cuts[i-1] is 0;
15+
else: the current cut num (first cut s[i-1:j+1] and then cut the rest
16+
s[j+1:]) is 1+cuts[j+1], compare it to the exisiting cuts[i-1], repalce if smaller.
17+
18+
is_palindrome[i][j]: whether s[i:j+1] is palindrome.
19+
Here we need not to compute the is_palindrome in advance.
20+
We use "Dynamic Programming" too, the formula is very intuitive:
21+
is_palindrome[i][j] = true if (is_palindrome[i+1][j-1] and s[i] == s[j]) else false
22+
23+
A better O(n) space solution can be found here:
24+
https://discuss.leetcode.com/topic/2840/my-solution-does-not-need-a-table-for-palindrome-is-it-right-it-uses-only-o-n-space
25+
*/
26+
27+
int minCut(string s) {
28+
if(s=="") return 0;
29+
int len = s.size();
30+
std::vector<int> cuts(len, 0);
31+
// Initialize the cuts array.
32+
for(int i=0; i<len; i++){
33+
cuts[i] = len - i-1;
34+
}
35+
std::vector<std::vector<bool>> is_palindrome(len, std::vector<bool>(len, false));
36+
for(int i=len-1; i>=0; i--){
37+
for(int j=i; j<len; j++){
38+
if((j-i<=2 && s[i]==s[j]) || (s[i]==s[j] && is_palindrome[i+1][j-1])){
39+
is_palindrome[i][j]=true;
40+
if(j==len-1){
41+
cuts[i]=0;
42+
}
43+
else{
44+
cuts[i] = min(cuts[i], cuts[j+1]+1);
45+
}
46+
}
47+
}
48+
}
49+
return cuts[0];
50+
}
51+
};
52+
53+
/*
54+
""
55+
"aab"
56+
"aabb"
57+
"aabaa"
58+
"acbca"
59+
"acbbca"
60+
*/

DynamicProgramming/132_PalindromePartitioningII.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
class Solution(object):
66
"""
77
Dynamic Programming:
8-
cuts[i]: minimum cuts needed for a palindrome partitioning of s[i:]
9-
is_palindrome[i][j]: whether s[i:i+1] is palindrome
8+
cuts[i]: minimum cuts needed for a palindrome partitioning of s[i:], so we want cuts[0].
9+
10+
To get cuts[i-1], we scan j from i-1 to len(s)-1.
11+
Once we comes to a is_palindrome[i-1][j]==true:
12+
if j==len(s)-1, the string s[i-1:] is a Pal, cuts[i-1] is 0;
13+
else: the current cut num (first cut s[i-1:j+1] and then cut the rest
14+
s[j+1:]) is 1+cuts[j+1], compare it to the exisiting cuts[i-1], repalce if smaller.
15+
16+
is_palindrome[i][j]: whether s[i:j+1] is palindrome.
17+
Here we need not to compute the is_palindrome in advance.
18+
We use "Dynamic Programming" too, the formula is very intuitive:
19+
is_palindrome[i][j] = true if (is_palindrome[i+1][j-1] and s[i] == s[j]) else false
20+
21+
A better O(n) space solution can be found here:
22+
https://discuss.leetcode.com/topic/2840/my-solution-does-not-need-a-table-for-palindrome-is-it-right-it-uses-only-o-n-space
1023
"""
24+
1125
def minCut(self, s):
1226
if not s:
1327
return 0
@@ -16,28 +30,27 @@ def minCut(self, s):
1630
is_palindrome = [[False for i in range(s_len)]
1731
for j in range(s_len)]
1832

19-
cuts = [s_len-1-i for i in range(s_len)]
20-
for i in range(s_len-1, -1, -1):
33+
cuts = [s_len - 1 - i for i in range(s_len)]
34+
for i in range(s_len - 1, -1, -1):
2135
for j in range(i, s_len):
2236
# if self.is_palindrome(i, j):
23-
if ((j-i < 2 and s[i] == s[j]) or
24-
(s[i] == s[j] and is_palindrome[i+1][j-1])):
37+
if ((j - i < 2 and s[i] == s[j]) or (s[i] == s[j] and is_palindrome[i + 1][j - 1])):
2538
is_palindrome[i][j] = True
2639
if j == s_len - 1:
2740
cuts[i] = 0
2841
else:
29-
cuts[i] = min(cuts[i], 1+cuts[j+1])
42+
cuts[i] = min(cuts[i], 1 + cuts[j + 1])
3043
else:
3144
pass
3245

3346
return cuts[0]
3447

48+
3549
"""
36-
if __name__ == "__main__":
37-
sol = Solution()
38-
print sol.minCut("aab")
39-
print sol.minCut("aabb")
40-
print sol.minCut("aabaa")
41-
print sol.minCut("acbca")
42-
print sol.minCut("acbbca")
50+
""
51+
"aab"
52+
"aabb"
53+
"aabaa"
54+
"acbca"
55+
"acbbca"
4356
"""

DynamicProgramming/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
# 优化
104104

105105
115.Distinct Subsequences
106-
106+
132
107107

108108
# 更多阅读
109109
[fibonacci数列为什么那么重要](https://www.zhihu.com/question/28062458)

0 commit comments

Comments
 (0)