Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0197502

Browse files
authored
Added task 698.
1 parent 0eff228 commit 0197502

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g0601_0700.s0698_partition_to_k_equal_sum_subsets;
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask #Memoization
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public boolean canPartitionKSubsets(int[] nums, int k) {
9+
if (nums == null || nums.length == 0) {
10+
return false;
11+
}
12+
int n = nums.length;
13+
int sum = 0;
14+
for (int num : nums) {
15+
sum += num;
16+
}
17+
if (sum % k != 0) {
18+
return false;
19+
}
20+
// sum of each subset = sum / k
21+
sum /= k;
22+
int[] dp = new int[1 << n];
23+
Arrays.fill(dp, -1);
24+
dp[0] = 0;
25+
for (int i = 0; i < (1 << n); i++) {
26+
if (dp[i] == -1) {
27+
continue;
28+
}
29+
int rem = sum - (dp[i] % sum);
30+
for (int j = 0; j < n; j++) {
31+
// bitmask
32+
int tmp = i | (1 << j);
33+
// skip if the bit is already taken
34+
if (tmp != i) {
35+
// num too big for current subset
36+
if (nums[j] > rem) {
37+
break;
38+
}
39+
// cumulative sum
40+
dp[tmp] = dp[i] + nums[j];
41+
}
42+
}
43+
}
44+
// true if total sum of all nums is the same
45+
return dp[(1 << n) - 1] == k * sum;
46+
}
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
698\. Partition to K Equal Sum Subsets
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return `true` if it is possible to divide this array into `k` non-empty subsets whose sums are all equal.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [4,3,2,3,5,2,1], k = 4
10+
11+
**Output:** true
12+
13+
**Explanation:** It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2,3,4], k = 3
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `1 <= k <= nums.length <= 16`
24+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
25+
* The frequency of each element is in the range `[1, 4]`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0601_0700.s0698_partition_to_k_equal_sum_subsets;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void canPartitionKSubsets() {
11+
assertThat(
12+
new Solution().canPartitionKSubsets(new int[] {4, 3, 2, 3, 5, 2, 1}, 4),
13+
equalTo(true));
14+
}
15+
16+
@Test
17+
void canPartitionKSubsets2() {
18+
assertThat(new Solution().canPartitionKSubsets(new int[] {1, 2, 3, 4}, 3), equalTo(false));
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /