Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0698
Browse files Browse the repository at this point in the history
No.0698.Partition to K Equal Sum Subsets
  • Loading branch information
maolonglong committed Jun 2, 2022
1 parent 631fa79 commit c04990a
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 2 deletions.
134 changes: 133 additions & 1 deletion solution/0600-0699/0698.Partition to K Equal Sum Subsets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,154 @@

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

解法和 [473. 火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) 相同

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
s = sum(nums)
target, m = divmod(s, k)
if m != 0:
return False

cur = [0] * k
n = len(nums)

def dfs(i: int) -> bool:
if i == n:
return True
for j in range(k):
if j > 0 and cur[j - 1] == cur[j]:
continue
cur[j] += nums[i]
if cur[j] <= target and dfs(i + 1):
return True
cur[j] -= nums[i]
return False

nums.sort(reverse=True)
return dfs(0)
```

### **Java**

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

```java
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum = (int) Arrays.stream(nums).sum();
if (sum % k != 0) {
return false;
}

Arrays.sort(nums);
int low = 0, high = nums.length - 1;
while (low < high) {
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
low++;
high--;
}
return dfs(nums, new int[k], 0, sum / k);
}

private boolean dfs(int[] nums, int[] cur, int i, int target) {
if (i == nums.length) {
return true;
}
for (int j = 0; j < cur.length; j++) {
if (j > 0 && cur[j - 1] == cur[j]) {
continue;
}
cur[j] += nums[i];
if (cur[j] <= target && dfs(nums, cur, i + 1, target)) {
return true;
}
cur[j] -= nums[i];
}
return false;
}
}
```

### **Go**

```go
func canPartitionKSubsets(nums []int, k int) bool {
sum := 0
for _, num := range nums {
sum += num
}
if sum%k != 0 {
return false
}

var (
target = sum / k
cur = make([]int, k)
n = len(nums)
)

var dfs func(i int) bool
dfs = func(i int) bool {
if i == n {
return true
}
for j := 0; j < k; j++ {
if j > 0 && cur[j-1] == cur[j] {
continue
}
cur[j] += nums[i]
if cur[j] <= target && dfs(i+1) {
return true
}
cur[j] -= nums[i]
}
return false
}

sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return dfs(0)
}
```

### **C++**

```cpp
class Solution {
public:
bool canPartitionKSubsets(vector<int>& nums, int k) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % k != 0) return false;

int target = sum / k;
int n = nums.size();
vector<int> cur(k, 0);

function<bool(int)> dfs;
dfs = [&](int i) {
if (i == n) return true;
for (int j = 0; j < k; ++j) {
if (j > 0 && cur[j - 1] == cur[j]) continue;
cur[j] += nums[i];
if (cur[j] <= target && dfs(i + 1)) return true;
cur[j] -= nums[i];
}
return false;
};

sort(nums.begin(), nums.end(), greater<int>());
return dfs(0);
}
};
```
### **...**
Expand Down
132 changes: 131 additions & 1 deletion solution/0600-0699/0698.Partition to K Equal Sum Subsets/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,143 @@
### **Python3**

```python

class Solution:
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
s = sum(nums)
target, m = divmod(s, k)
if m != 0:
return False

cur = [0] * k
n = len(nums)

def dfs(i: int) -> bool:
if i == n:
return True
for j in range(k):
if j > 0 and cur[j - 1] == cur[j]:
continue
cur[j] += nums[i]
if cur[j] <= target and dfs(i + 1):
return True
cur[j] -= nums[i]
return False

nums.sort(reverse=True)
return dfs(0)
```

### **Java**

```java
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum = (int) Arrays.stream(nums).sum();
if (sum % k != 0) {
return false;
}

Arrays.sort(nums);
int low = 0, high = nums.length - 1;
while (low < high) {
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
low++;
high--;
}
return dfs(nums, new int[k], 0, sum / k);
}

private boolean dfs(int[] nums, int[] cur, int i, int target) {
if (i == nums.length) {
return true;
}
for (int j = 0; j < cur.length; j++) {
if (j > 0 && cur[j - 1] == cur[j]) {
continue;
}
cur[j] += nums[i];
if (cur[j] <= target && dfs(nums, cur, i + 1, target)) {
return true;
}
cur[j] -= nums[i];
}
return false;
}
}
```

### **Go**

```go
func canPartitionKSubsets(nums []int, k int) bool {
sum := 0
for _, num := range nums {
sum += num
}
if sum%k != 0 {
return false
}

var (
target = sum / k
cur = make([]int, k)
n = len(nums)
)

var dfs func(i int) bool
dfs = func(i int) bool {
if i == n {
return true
}
for j := 0; j < k; j++ {
if j > 0 && cur[j-1] == cur[j] {
continue
}
cur[j] += nums[i]
if cur[j] <= target && dfs(i+1) {
return true
}
cur[j] -= nums[i]
}
return false
}

sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return dfs(0)
}
```

### **C++**

```cpp
class Solution {
public:
bool canPartitionKSubsets(vector<int>& nums, int k) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % k != 0) return false;

int target = sum / k;
int n = nums.size();
vector<int> cur(k, 0);

function<bool(int)> dfs;
dfs = [&](int i) {
if (i == n) return true;
for (int j = 0; j < k; ++j) {
if (j > 0 && cur[j - 1] == cur[j]) continue;
cur[j] += nums[i];
if (cur[j] <= target && dfs(i + 1)) return true;
cur[j] -= nums[i];
}
return false;
};

sort(nums.begin(), nums.end(), greater<int>());
return dfs(0);
}
};
```
### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
bool canPartitionKSubsets(vector<int>& nums, int k) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % k != 0) return false;

int target = sum / k;
int n = nums.size();
vector<int> cur(k, 0);

function<bool(int)> dfs;
dfs = [&](int i) {
if (i == n) return true;
for (int j = 0; j < k; ++j) {
if (j > 0 && cur[j - 1] == cur[j]) continue;
cur[j] += nums[i];
if (cur[j] <= target && dfs(i + 1)) return true;
cur[j] -= nums[i];
}
return false;
};

sort(nums.begin(), nums.end(), greater<int>());
return dfs(0);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
func canPartitionKSubsets(nums []int, k int) bool {
sum := 0
for _, num := range nums {
sum += num
}
if sum%k != 0 {
return false
}

var (
target = sum / k
cur = make([]int, k)
n = len(nums)
)

var dfs func(i int) bool
dfs = func(i int) bool {
if i == n {
return true
}
for j := 0; j < k; j++ {
if j > 0 && cur[j-1] == cur[j] {
continue
}
cur[j] += nums[i]
if cur[j] <= target && dfs(i+1) {
return true
}
cur[j] -= nums[i]
}
return false
}

sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return dfs(0)
}
Loading

0 comments on commit c04990a

Please sign in to comment.