forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0698
No.0698.Partition to K Equal Sum Subsets
- Loading branch information
1 parent
631fa79
commit c04990a
Showing
6 changed files
with
386 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
solution/0600-0699/0698.Partition to K Equal Sum Subsets/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
solution/0600-0699/0698.Partition to K Equal Sum Subsets/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.