Skip to content

Commit c04990a

Browse files
committed
feat: add solutions to lc problem: No.0698
No.0698.Partition to K Equal Sum Subsets
1 parent 631fa79 commit c04990a

File tree

6 files changed

+386
-2
lines changed

6 files changed

+386
-2
lines changed

solution/0600-0699/0698.Partition to K Equal Sum Subsets/README.md

+133-1
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,154 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
解法和 [473. 火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) 相同
41+
4042
<!-- tabs:start -->
4143

4244
### **Python3**
4345

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

4648
```python
47-
49+
class Solution:
50+
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
51+
s = sum(nums)
52+
target, m = divmod(s, k)
53+
if m != 0:
54+
return False
55+
56+
cur = [0] * k
57+
n = len(nums)
58+
59+
def dfs(i: int) -> bool:
60+
if i == n:
61+
return True
62+
for j in range(k):
63+
if j > 0 and cur[j - 1] == cur[j]:
64+
continue
65+
cur[j] += nums[i]
66+
if cur[j] <= target and dfs(i + 1):
67+
return True
68+
cur[j] -= nums[i]
69+
return False
70+
71+
nums.sort(reverse=True)
72+
return dfs(0)
4873
```
4974

5075
### **Java**
5176

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

5479
```java
80+
class Solution {
81+
public boolean canPartitionKSubsets(int[] nums, int k) {
82+
int sum = (int) Arrays.stream(nums).sum();
83+
if (sum % k != 0) {
84+
return false;
85+
}
86+
87+
Arrays.sort(nums);
88+
int low = 0, high = nums.length - 1;
89+
while (low < high) {
90+
int temp = nums[low];
91+
nums[low] = nums[high];
92+
nums[high] = temp;
93+
low++;
94+
high--;
95+
}
96+
return dfs(nums, new int[k], 0, sum / k);
97+
}
98+
99+
private boolean dfs(int[] nums, int[] cur, int i, int target) {
100+
if (i == nums.length) {
101+
return true;
102+
}
103+
for (int j = 0; j < cur.length; j++) {
104+
if (j > 0 && cur[j - 1] == cur[j]) {
105+
continue;
106+
}
107+
cur[j] += nums[i];
108+
if (cur[j] <= target && dfs(nums, cur, i + 1, target)) {
109+
return true;
110+
}
111+
cur[j] -= nums[i];
112+
}
113+
return false;
114+
}
115+
}
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func canPartitionKSubsets(nums []int, k int) bool {
122+
sum := 0
123+
for _, num := range nums {
124+
sum += num
125+
}
126+
if sum%k != 0 {
127+
return false
128+
}
129+
130+
var (
131+
target = sum / k
132+
cur = make([]int, k)
133+
n = len(nums)
134+
)
135+
136+
var dfs func(i int) bool
137+
dfs = func(i int) bool {
138+
if i == n {
139+
return true
140+
}
141+
for j := 0; j < k; j++ {
142+
if j > 0 && cur[j-1] == cur[j] {
143+
continue
144+
}
145+
cur[j] += nums[i]
146+
if cur[j] <= target && dfs(i+1) {
147+
return true
148+
}
149+
cur[j] -= nums[i]
150+
}
151+
return false
152+
}
153+
154+
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
155+
return dfs(0)
156+
}
157+
```
55158

159+
### **C++**
160+
161+
```cpp
162+
class Solution {
163+
public:
164+
bool canPartitionKSubsets(vector<int>& nums, int k) {
165+
int sum = accumulate(nums.begin(), nums.end(), 0);
166+
if (sum % k != 0) return false;
167+
168+
int target = sum / k;
169+
int n = nums.size();
170+
vector<int> cur(k, 0);
171+
172+
function<bool(int)> dfs;
173+
dfs = [&](int i) {
174+
if (i == n) return true;
175+
for (int j = 0; j < k; ++j) {
176+
if (j > 0 && cur[j - 1] == cur[j]) continue;
177+
cur[j] += nums[i];
178+
if (cur[j] <= target && dfs(i + 1)) return true;
179+
cur[j] -= nums[i];
180+
}
181+
return false;
182+
};
183+
184+
sort(nums.begin(), nums.end(), greater<int>());
185+
return dfs(0);
186+
}
187+
};
56188
```
57189
58190
### **...**

solution/0600-0699/0698.Partition to K Equal Sum Subsets/README_EN.md

+131-1
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,143 @@
3838
### **Python3**
3939

4040
```python
41-
41+
class Solution:
42+
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
43+
s = sum(nums)
44+
target, m = divmod(s, k)
45+
if m != 0:
46+
return False
47+
48+
cur = [0] * k
49+
n = len(nums)
50+
51+
def dfs(i: int) -> bool:
52+
if i == n:
53+
return True
54+
for j in range(k):
55+
if j > 0 and cur[j - 1] == cur[j]:
56+
continue
57+
cur[j] += nums[i]
58+
if cur[j] <= target and dfs(i + 1):
59+
return True
60+
cur[j] -= nums[i]
61+
return False
62+
63+
nums.sort(reverse=True)
64+
return dfs(0)
4265
```
4366

4467
### **Java**
4568

4669
```java
70+
class Solution {
71+
public boolean canPartitionKSubsets(int[] nums, int k) {
72+
int sum = (int) Arrays.stream(nums).sum();
73+
if (sum % k != 0) {
74+
return false;
75+
}
76+
77+
Arrays.sort(nums);
78+
int low = 0, high = nums.length - 1;
79+
while (low < high) {
80+
int temp = nums[low];
81+
nums[low] = nums[high];
82+
nums[high] = temp;
83+
low++;
84+
high--;
85+
}
86+
return dfs(nums, new int[k], 0, sum / k);
87+
}
88+
89+
private boolean dfs(int[] nums, int[] cur, int i, int target) {
90+
if (i == nums.length) {
91+
return true;
92+
}
93+
for (int j = 0; j < cur.length; j++) {
94+
if (j > 0 && cur[j - 1] == cur[j]) {
95+
continue;
96+
}
97+
cur[j] += nums[i];
98+
if (cur[j] <= target && dfs(nums, cur, i + 1, target)) {
99+
return true;
100+
}
101+
cur[j] -= nums[i];
102+
}
103+
return false;
104+
}
105+
}
106+
```
107+
108+
### **Go**
109+
110+
```go
111+
func canPartitionKSubsets(nums []int, k int) bool {
112+
sum := 0
113+
for _, num := range nums {
114+
sum += num
115+
}
116+
if sum%k != 0 {
117+
return false
118+
}
119+
120+
var (
121+
target = sum / k
122+
cur = make([]int, k)
123+
n = len(nums)
124+
)
125+
126+
var dfs func(i int) bool
127+
dfs = func(i int) bool {
128+
if i == n {
129+
return true
130+
}
131+
for j := 0; j < k; j++ {
132+
if j > 0 && cur[j-1] == cur[j] {
133+
continue
134+
}
135+
cur[j] += nums[i]
136+
if cur[j] <= target && dfs(i+1) {
137+
return true
138+
}
139+
cur[j] -= nums[i]
140+
}
141+
return false
142+
}
143+
144+
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
145+
return dfs(0)
146+
}
147+
```
47148

149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
bool canPartitionKSubsets(vector<int>& nums, int k) {
155+
int sum = accumulate(nums.begin(), nums.end(), 0);
156+
if (sum % k != 0) return false;
157+
158+
int target = sum / k;
159+
int n = nums.size();
160+
vector<int> cur(k, 0);
161+
162+
function<bool(int)> dfs;
163+
dfs = [&](int i) {
164+
if (i == n) return true;
165+
for (int j = 0; j < k; ++j) {
166+
if (j > 0 && cur[j - 1] == cur[j]) continue;
167+
cur[j] += nums[i];
168+
if (cur[j] <= target && dfs(i + 1)) return true;
169+
cur[j] -= nums[i];
170+
}
171+
return false;
172+
};
173+
174+
sort(nums.begin(), nums.end(), greater<int>());
175+
return dfs(0);
176+
}
177+
};
48178
```
49179
50180
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
bool canPartitionKSubsets(vector<int>& nums, int k) {
4+
int sum = accumulate(nums.begin(), nums.end(), 0);
5+
if (sum % k != 0) return false;
6+
7+
int target = sum / k;
8+
int n = nums.size();
9+
vector<int> cur(k, 0);
10+
11+
function<bool(int)> dfs;
12+
dfs = [&](int i) {
13+
if (i == n) return true;
14+
for (int j = 0; j < k; ++j) {
15+
if (j > 0 && cur[j - 1] == cur[j]) continue;
16+
cur[j] += nums[i];
17+
if (cur[j] <= target && dfs(i + 1)) return true;
18+
cur[j] -= nums[i];
19+
}
20+
return false;
21+
};
22+
23+
sort(nums.begin(), nums.end(), greater<int>());
24+
return dfs(0);
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
func canPartitionKSubsets(nums []int, k int) bool {
2+
sum := 0
3+
for _, num := range nums {
4+
sum += num
5+
}
6+
if sum%k != 0 {
7+
return false
8+
}
9+
10+
var (
11+
target = sum / k
12+
cur = make([]int, k)
13+
n = len(nums)
14+
)
15+
16+
var dfs func(i int) bool
17+
dfs = func(i int) bool {
18+
if i == n {
19+
return true
20+
}
21+
for j := 0; j < k; j++ {
22+
if j > 0 && cur[j-1] == cur[j] {
23+
continue
24+
}
25+
cur[j] += nums[i]
26+
if cur[j] <= target && dfs(i+1) {
27+
return true
28+
}
29+
cur[j] -= nums[i]
30+
}
31+
return false
32+
}
33+
34+
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
35+
return dfs(0)
36+
}

0 commit comments

Comments
 (0)