Skip to content

Commit 666ef10

Browse files
[N-0] add 698
1 parent 29d368a commit 666ef10

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|698|[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_698.java) | O(n*(2^n)) | O(2^n) | Medium | Backtracking
2526
|697|[Degree of an Array](https://leetcode.com/problems/degree-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_697.java) | O(n) | O(n) | Easy |
2627
|696|[Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_696.java) | O(n) | O(n) | Easy |
2728
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_695.java) | O(m*n) | O(1) | Easy | DFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 698. Partition to K Equal Sum Subsets
5+
*
6+
* Given an array of integers nums and a positive integer k,
7+
* find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
8+
9+
Example 1:
10+
11+
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
12+
Output: True
13+
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
14+
15+
Note:
16+
1 <= k <= len(nums) <= 16.
17+
0 < nums[i] < 10000.
18+
*/
19+
public class _698 {
20+
21+
public static class Solution1 {
22+
public boolean canPartitionKSubsets(int[] nums, int k) {
23+
long sum = 0;
24+
for (int num : nums) {
25+
sum += num;
26+
}
27+
if (sum % k != 0) {
28+
return false;
29+
}
30+
int equalSum = (int) (sum / k);
31+
boolean[] visited = new boolean[nums.length];
32+
return canPartition(nums, visited, 0, k, 0, 0, equalSum);
33+
}
34+
35+
private boolean canPartition(int[] nums, boolean[] visited, int startIndex, int k, int currSum, int currNum, int target) {
36+
if (k == 1) {
37+
return true;
38+
}
39+
if (currSum == target && currNum > 0) {
40+
/**Everytime when we get currSum == target, we'll start from index 0 and look up the numbers that are not used yet
41+
* and try to find another sum that could equal to target*/
42+
return canPartition(nums, visited, 0, k - 1, 0, 0, target);
43+
}
44+
for (int i = startIndex; i < nums.length; i++) {
45+
if (!visited[i]) {
46+
visited[i] = true;
47+
if (canPartition(nums, visited, i + 1, k, currSum + nums[i], currNum++, target)) {
48+
return true;
49+
}
50+
visited[i] = false;
51+
}
52+
}
53+
return false;
54+
}
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._698;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _698Test {
10+
private static _698.Solution1 solution1;
11+
private static int[] nums;
12+
private static int k;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _698.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
nums = new int[]{4, 3, 2, 3, 5, 2, 1};
22+
k = 4;
23+
assertEquals(true, solution1.canPartitionKSubsets(nums, k));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
nums = new int[]{-1,1,0,0};
29+
k = 4;
30+
assertEquals(false, solution1.canPartitionKSubsets(nums, k));
31+
}
32+
}

0 commit comments

Comments
 (0)