forked from soapyigu/LeetCode-Swift
-
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.
[DFS] Add a solution to Partition to K Equal Sum Subsets
- Loading branch information
Showing
2 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/ | ||
* Primary idea: Divide the whole array into buckets and use | ||
* DFS to check whether there is a valid solution | ||
* | ||
* Time Complexity: O(k^n), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class PartitionKEqualSumSubsets { | ||
func canPartitionKSubsets(_ nums: [Int], _ k: Int) -> Bool { | ||
let sum = nums.reduce(0) { $0 + $1 } | ||
|
||
guard sum % k == 0 else { | ||
return false | ||
} | ||
|
||
let target = sum / k, nums = nums.sorted() | ||
var index = nums.count - 1, groupsEqualToK = Array(repeating: 0, count: k) | ||
|
||
guard nums[index] <= target else { | ||
return false | ||
} | ||
|
||
return dfs(&groupsEqualToK, target, nums, index) | ||
} | ||
|
||
private func dfs(_ groupsEqualToK: inout [Int], _ target: Int, _ nums: [Int], _ index: Int) -> Bool { | ||
if index < 0 { | ||
return true | ||
} | ||
|
||
let currentNum = nums[index] | ||
|
||
for i in 0..<groupsEqualToK.count { | ||
if groupsEqualToK[i] + currentNum <= target { | ||
groupsEqualToK[i] += currentNum | ||
|
||
if dfs(&groupsEqualToK, target, nums, index - 1) { | ||
return true | ||
} | ||
|
||
groupsEqualToK[i] -= currentNum | ||
} | ||
|
||
if groupsEqualToK[i] == 0 { | ||
break | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
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