Skip to content

Commit 976a940

Browse files
committed
feat: add solutions to lc problem: No.0132
No.0132.Palindrome Partitioning II
1 parent 66cbceb commit 976a940

File tree

3 files changed

+297
-4
lines changed

3 files changed

+297
-4
lines changed

solution/0100-0199/0132.Palindrome Partitioning II/README.md

+146
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@
5959

6060
<!-- 这里可写当前语言的特殊实现逻辑 -->
6161

62+
```python
63+
class Solution:
64+
def minCut(self, s: str) -> int:
65+
@cache
66+
def dfs(i):
67+
if i >= n - 1:
68+
return 0
69+
ans = inf
70+
for j in range(i, n):
71+
if g[i][j]:
72+
ans = min(ans, dfs(j + 1) + (j < n - 1))
73+
return ans
74+
75+
n = len(s)
76+
g = [[True] * n for _ in range(n)]
77+
for i in range(n - 1, -1, -1):
78+
for j in range(i + 1, n):
79+
g[i][j] = s[i] == s[j] and g[i + 1][j - 1]
80+
ans = dfs(0)
81+
dfs.cache_clear()
82+
return ans
83+
```
84+
6285
```python
6386
class Solution:
6487
def minCut(self, s: str) -> int:
@@ -81,6 +104,49 @@ class Solution:
81104

82105
<!-- 这里可写当前语言的特殊实现逻辑 -->
83106

107+
```java
108+
class Solution {
109+
private boolean[][] g;
110+
private int[] f;
111+
private String s;
112+
private int n;
113+
114+
public int minCut(String s) {
115+
n = s.length();
116+
g = new boolean[n][n];
117+
for (var e : g) {
118+
Arrays.fill(e, true);
119+
}
120+
for (int i = n - 1; i >= 0; --i) {
121+
for (int j = i + 1; j < n; ++j) {
122+
g[i][j] = s.charAt(i) == s.charAt(j) && g[i + 1][j - 1];
123+
}
124+
}
125+
this.s = s;
126+
f = new int[n];
127+
Arrays.fill(f, -1);
128+
return dfs(0);
129+
}
130+
131+
private int dfs(int i) {
132+
if (i >= n - 1) {
133+
return 0;
134+
}
135+
if (f[i] != -1) {
136+
return f[i];
137+
}
138+
int ans = Integer.MAX_VALUE;
139+
for (int j = i; j < n; ++j) {
140+
if (g[i][j]) {
141+
ans = Math.min(ans, dfs(j + 1) + (j < n - 1 ? 1 : 0));
142+
}
143+
}
144+
f[i] = ans;
145+
return ans;
146+
}
147+
}
148+
```
149+
84150
```java
85151
class Solution {
86152
public int minCut(String s) {
@@ -109,6 +175,56 @@ class Solution {
109175

110176
### **Go**
111177

178+
179+
```go
180+
func minCut(s string) int {
181+
n := len(s)
182+
f := make([]int, n)
183+
g := make([][]bool, n)
184+
for i := range g {
185+
f[i] = -1
186+
g[i] = make([]bool, n)
187+
for j := range g[i] {
188+
g[i][j] = true
189+
}
190+
}
191+
for i := n - 1; i >= 0; i-- {
192+
for j := i + 1; j < n; j++ {
193+
g[i][j] = s[i] == s[j] && g[i+1][j-1]
194+
}
195+
}
196+
var dfs func(i int) int
197+
dfs = func(i int) int {
198+
if i >= n-1 {
199+
return 0
200+
}
201+
if f[i] != -1 {
202+
return f[i]
203+
}
204+
ans := math.MaxInt32
205+
for j := i; j < n; j++ {
206+
if g[i][j] {
207+
t := 1
208+
if j == n-1 {
209+
t = 0
210+
}
211+
ans = min(ans, dfs(j+1)+t)
212+
}
213+
}
214+
f[i] = ans
215+
return ans
216+
}
217+
return dfs(0)
218+
}
219+
220+
func min(a, b int) int {
221+
if a < b {
222+
return a
223+
}
224+
return b
225+
}
226+
```
227+
112228
```go
113229
func minCut(s string) int {
114230
n := len(s)
@@ -145,6 +261,36 @@ func min(x, y int) int {
145261

146262
### **C++**
147263

264+
```cpp
265+
class Solution {
266+
public:
267+
int minCut(string s) {
268+
int n = s.size();
269+
vector<vector<bool>> g(n, vector<bool>(n, true));
270+
for (int i = n - 1; i >= 0; --i) {
271+
for (int j = i + 1; j < n; ++j) {
272+
g[i][j] = s[i] == s[j] && g[i + 1][j - 1];
273+
}
274+
}
275+
vector<int> f(n, -1);
276+
function<int(int)> dfs;
277+
dfs = [&](int i) {
278+
if (i >= n - 1) return 0;
279+
if (f[i] != -1) return f[i];
280+
int ans = INT_MAX;
281+
for (int j = i; j < n; ++j) {
282+
if (g[i][j]) {
283+
ans = min(ans, dfs(j + 1) + (j < n - 1));
284+
}
285+
}
286+
f[i] = ans;
287+
return ans;
288+
};
289+
return dfs(0);
290+
}
291+
};
292+
```
293+
148294
```cpp
149295
class Solution {
150296
public:

solution/0100-0199/0132.Palindrome Partitioning II/README_EN.md

+145
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@
4545

4646
### **Python3**
4747

48+
```python
49+
class Solution:
50+
def minCut(self, s: str) -> int:
51+
@cache
52+
def dfs(i):
53+
if i >= n - 1:
54+
return 0
55+
ans = inf
56+
for j in range(i, n):
57+
if g[i][j]:
58+
ans = min(ans, dfs(j + 1) + (j < n - 1))
59+
return ans
60+
61+
n = len(s)
62+
g = [[True] * n for _ in range(n)]
63+
for i in range(n - 1, -1, -1):
64+
for j in range(i + 1, n):
65+
g[i][j] = s[i] == s[j] and g[i + 1][j - 1]
66+
ans = dfs(0)
67+
dfs.cache_clear()
68+
return ans
69+
```
70+
4871
```python
4972
class Solution:
5073
def minCut(self, s: str) -> int:
@@ -65,6 +88,49 @@ class Solution:
6588

6689
### **Java**
6790

91+
```java
92+
class Solution {
93+
private boolean[][] g;
94+
private int[] f;
95+
private String s;
96+
private int n;
97+
98+
public int minCut(String s) {
99+
n = s.length();
100+
g = new boolean[n][n];
101+
for (var e : g) {
102+
Arrays.fill(e, true);
103+
}
104+
for (int i = n - 1; i >= 0; --i) {
105+
for (int j = i + 1; j < n; ++j) {
106+
g[i][j] = s.charAt(i) == s.charAt(j) && g[i + 1][j - 1];
107+
}
108+
}
109+
this.s = s;
110+
f = new int[n];
111+
Arrays.fill(f, -1);
112+
return dfs(0);
113+
}
114+
115+
private int dfs(int i) {
116+
if (i >= n - 1) {
117+
return 0;
118+
}
119+
if (f[i] != -1) {
120+
return f[i];
121+
}
122+
int ans = Integer.MAX_VALUE;
123+
for (int j = i; j < n; ++j) {
124+
if (g[i][j]) {
125+
ans = Math.min(ans, dfs(j + 1) + (j < n - 1 ? 1 : 0));
126+
}
127+
}
128+
f[i] = ans;
129+
return ans;
130+
}
131+
}
132+
```
133+
68134
```java
69135
class Solution {
70136
public int minCut(String s) {
@@ -93,6 +159,55 @@ class Solution {
93159

94160
### **Go**
95161

162+
```go
163+
func minCut(s string) int {
164+
n := len(s)
165+
f := make([]int, n)
166+
g := make([][]bool, n)
167+
for i := range g {
168+
f[i] = -1
169+
g[i] = make([]bool, n)
170+
for j := range g[i] {
171+
g[i][j] = true
172+
}
173+
}
174+
for i := n - 1; i >= 0; i-- {
175+
for j := i + 1; j < n; j++ {
176+
g[i][j] = s[i] == s[j] && g[i+1][j-1]
177+
}
178+
}
179+
var dfs func(i int) int
180+
dfs = func(i int) int {
181+
if i >= n-1 {
182+
return 0
183+
}
184+
if f[i] != -1 {
185+
return f[i]
186+
}
187+
ans := math.MaxInt32
188+
for j := i; j < n; j++ {
189+
if g[i][j] {
190+
t := 1
191+
if j == n-1 {
192+
t = 0
193+
}
194+
ans = min(ans, dfs(j+1)+t)
195+
}
196+
}
197+
f[i] = ans
198+
return ans
199+
}
200+
return dfs(0)
201+
}
202+
203+
func min(a, b int) int {
204+
if a < b {
205+
return a
206+
}
207+
return b
208+
}
209+
```
210+
96211
```go
97212
func minCut(s string) int {
98213
n := len(s)
@@ -129,6 +244,36 @@ func min(x, y int) int {
129244

130245
### **C++**
131246

247+
```cpp
248+
class Solution {
249+
public:
250+
int minCut(string s) {
251+
int n = s.size();
252+
vector<vector<bool>> g(n, vector<bool>(n, true));
253+
for (int i = n - 1; i >= 0; --i) {
254+
for (int j = i + 1; j < n; ++j) {
255+
g[i][j] = s[i] == s[j] && g[i + 1][j - 1];
256+
}
257+
}
258+
vector<int> f(n, -1);
259+
function<int(int)> dfs;
260+
dfs = [&](int i) {
261+
if (i >= n - 1) return 0;
262+
if (f[i] != -1) return f[i];
263+
int ans = INT_MAX;
264+
for (int j = i; j < n; ++j) {
265+
if (g[i][j]) {
266+
ans = min(ans, dfs(j + 1) + (j < n - 1));
267+
}
268+
}
269+
f[i] = ans;
270+
return ans;
271+
};
272+
return dfs(0);
273+
}
274+
};
275+
```
276+
132277
```cpp
133278
class Solution {
134279
public:

solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ A = [1, 3, 5, 7] , B = [1, 2, 3, 4]
5757

5858
**方法一:动态规划**
5959

60-
定义 $a$, $b$ 分别表示当前位置不交换和交换的最小交换次数,当 $i=0$ 时,有 $a=0, b=1$
60+
定义 $a$, $b$ 分别表示使得前 $i$ 个元素序列严格递增,且第 $i$ 个元素不交换、交换的最小交换次数
6161

62-
对于 $i\gt 0$,我们先将此时 $a$, $b$ 的值保存在 $x$, $y$ 中,然后分情况讨论:
62+
$i=0$ 时,易知 $a = 0$, $b=1$。
6363

64-
如果 $nums1[i - 1] \ge nums1[i]$ 或者 $nums2[i - 1] \ge nums2[i]$,那么下标 $i-1$ 和 $i$ 对应的元素的相对位置必须发生变化,此时,如果前一个位置交换了,那么当前位置不交换,因此有 $a = y$;如果前一个位置没有交换,那么当前位置必须交换,因此有 $b = x + 1$。
64+
当 $i\gt 0$ 时,我们先将此前 $a$, $b$ 的值保存在 $x$, $y$ 中,然后分情况讨论:
6565

66-
否则,下标 $i-1$ 和 $i$ 对应的元素的相对位置可以不发生变化,那么有 $b=y+1$;另外,如果满足 $nums1[i - 1] \lt nums2[i]$ 并且 $nums2[i - 1] \lt nums1[i]$,那么下标 $i-1$ 和 $i$ 对应的元素的相对位置可以发生变化。此时 $a$ 和 $b$ 可以取较小值,因此有 $a = \min(a, y)$ 和 $b = \min(b, x + 1)$。
66+
如果 $nums1[i - 1] \ge nums1[i]$ 或者 $nums2[i - 1] \ge nums2[i]$,为了使得两个序列均严格递增,下标 $i-1$ 和 $i$ 对应的元素的相对位置必须发生变化。也就是说,如果前一个位置交换了,那么当前位置不交换,因此有 $a = y$;如果前一个位置没有交换,那么当前位置必须交换,因此有 $b = x + 1$。
67+
68+
否则,下标 $i-1$ 和 $i$ 对应的元素的相对位置可以不发生变化,那么有 $b = y + 1$。另外,如果满足 $nums1[i - 1] \lt nums2[i]$ 并且 $nums2[i - 1] \lt nums1[i]$,那么下标 $i-1$ 和 $i$ 对应的元素的相对位置可以发生变化,此时 $a$ 和 $b$ 可以取较小值,因此有 $a = \min(a, y)$ 和 $b = \min(b, x + 1)$。
6769

6870
最后,返回 $a$ 和 $b$ 中较小值即可。
6971

0 commit comments

Comments
 (0)