Skip to content

Commit 47a56fb

Browse files
committed
[DFS] Add a solution to Partition to K Equal Sum Subsets
1 parent ea7827a commit 47a56fb

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

DFS/PartitionKEqualSumSubsets.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
3+
* Primary idea: Divide the whole array into buckets and use
4+
* DFS to check whether there is a valid solution
5+
*
6+
* Time Complexity: O(k^n), Space Complexity: O(n)
7+
*
8+
*/
9+
10+
class PartitionKEqualSumSubsets {
11+
func canPartitionKSubsets(_ nums: [Int], _ k: Int) -> Bool {
12+
let sum = nums.reduce(0) { $0 + $1 }
13+
14+
guard sum % k == 0 else {
15+
return false
16+
}
17+
18+
let target = sum / k, nums = nums.sorted()
19+
var index = nums.count - 1, groupsEqualToK = Array(repeating: 0, count: k)
20+
21+
guard nums[index] <= target else {
22+
return false
23+
}
24+
25+
return dfs(&groupsEqualToK, target, nums, index)
26+
}
27+
28+
private func dfs(_ groupsEqualToK: inout [Int], _ target: Int, _ nums: [Int], _ index: Int) -> Bool {
29+
if index < 0 {
30+
return true
31+
}
32+
33+
let currentNum = nums[index]
34+
35+
for i in 0..<groupsEqualToK.count {
36+
if groupsEqualToK[i] + currentNum <= target {
37+
groupsEqualToK[i] += currentNum
38+
39+
if dfs(&groupsEqualToK, target, nums, index - 1) {
40+
return true
41+
}
42+
43+
groupsEqualToK[i] -= currentNum
44+
}
45+
46+
if groupsEqualToK[i] == 0 {
47+
break
48+
}
49+
}
50+
51+
return false
52+
}
53+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 319 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 320 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -287,6 +287,7 @@
287287
[Word Search](https://leetcode.com/problems/word-search/)| [Swift](./DFS/WordSearch.swift)| Medium| O((mn * 4 ^ (k - 1))| O(mn)|
288288
[Word Search II](https://leetcode.com/problems/word-search-ii/)| [Swift](./DFS/WordSearchII.swift)| Hard| O(((mn)^2))| O(n^2)|
289289
[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [Swift](./DFS/WordDictionary.swift)| Medium| O(n)| O(n)|
290+
[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)| [Swift](./DFS/PartitionKEqualSumSubsets.swift)| Medium| O(k^n)| O(n)|
290291
[N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n^n))| O(n^2)|
291292
[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n^n))| O(n)|
292293
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n^n))| O(n)|

0 commit comments

Comments
 (0)