Partition to K Equal Sum Subsets

Given an array of integers nums and a positive integer k, find whether it‘s possible to divide this array into k non-empty subsets whose sums are all equal.

Example 1:

Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It‘s possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.

Note:

  • 1 <= k <= len(nums) <= 16.
  • 0 < nums[i] < 10000.

Assume sum is the sum of nums[] . The dfs process is to find a subset of nums[] which sum equals to sum/k. We use an array visited[] to record which element in nums[] is used. Each time when we get a cur_sum = sum/k, we will start from position 0 in nums[] to look up the elements that are not used yet and find another cur_sum = sum/k.

 1 class Solution {
 2     public boolean canPartitionKSubsets(int[] nums, int k) {
 3         int sum = 0;
 4         for (int num : nums) sum += num;
 5         if (k <= 0 || sum % k != 0) return false;
 6         int[] visited = new int[nums.length];
 7         return canPartition(nums, visited, 0, k, 0, 0, sum / k);
 8     }
 9
10     public boolean canPartition(int[] nums, int[] visited, int start_index, int k, int cur_sum, int cur_num, int target) {
11         if (k == 1) return true;
12         if (cur_sum == target && cur_num > 0) return canPartition(nums, visited, 0, k - 1, 0, 0, target);
13         for (int i = start_index; i < nums.length; i++) {
14             if (visited[i] == 0) {
15                 visited[i] = 1;
16                 if (canPartition(nums, visited, i + 1, k, cur_sum + nums[i], cur_num++, target)) {
17                     return true;
18                 }
19                 visited[i] = 0;
20             }
21         }
22         return false;
23     }
24 }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/10817356.html

时间: 2024-08-30 13:31:20

Partition to K Equal Sum Subsets的相关文章

[Swift]LeetCode698. 划分为k个相等的子集 | Partition to K Equal Sum Subsets

Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible

698. Partition to K Equal Sum Subsets

问题描述: Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's po

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

HDU-3280 Equal Sum Partitions

http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 453    Accepted Submission(s): 337 Problem Description An equal sum

[LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

6661 Equal Sum Sets(DP)

Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of se

UvaLive6661 Equal Sum Sets dfs或dp

UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cs

D。6661 - Equal Sum Sets

Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elements of a set aredifferent. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} meanthe same set.Specifying the

Aizu 1335 Equal Sum Sets

Description Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesn't matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the