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.

Approach #1: DFS + Backtracking. [C++]

class Solution {
public:
bool canPartitionKSubsets(vector<int>& nums, int k) {
int len = nums.size();
if (k == 1) return true;
if (len < k) return false; int sum = 0;
for (int num : nums)
sum += num;
if (sum % k != 0) return false; int avg = sum / k;
vector<int> token(len+5, 0), subsets(k+5, 0);
subsets[0] = nums[len-1];
token[len-1] = 1;
return solve(nums, token, subsets, avg, k, len, 0, len-1);
} private:
bool solve(vector<int>& nums, vector<int>& token, vector<int>& subsets,
const int& avg, const int& k, const int& len, int curIdx, int limitIdx) {
if (subsets[curIdx] == avg) {
if (curIdx == k-2) return true;
return solve(nums, token, subsets, avg, k, len, curIdx+1, len-1);
} for (int i = limitIdx; i >= 0; --i) {
if (token[i] == 1) continue;
int tmp = subsets[curIdx] + nums[i]; if (tmp <= avg) {
subsets[curIdx] += nums[i];
token[i] = 1;
bool nxt = solve(nums, token, subsets, avg, k, len, curIdx, i-1);
subsets[curIdx] -= nums[i];
token[i] = 0;
if (nxt) return true;
}
} return false;
}
};

  

Analysis:

We can solve this problem recursively, we keep an array for sum of each partition and a array to check whether an element is already taken into some partition or not.

First we need to check some base cases:

If K is 1, then we already have our answer, complete array is only sbset with same sum.

If N < K, then it is not possible to divide array into subsets with equal sum, because we can't divide the array into more than N parts.

If sum of array is not divisible by K. then it is not possible to divide the array. We will proceed only if k divides sum. Our goal reduces to divide array into K parts where sum of each part should be array_sum / k

In above code  a recursive method is written which tries to add array element into some subset. If sum of this subset reaches required sum, we iterator for next part recursively, otherwise we backtrack for different set of elements. If number of subsets whose sum reaches the required sum is (K-1), we flag that it is possible to partition array nto K parts with equal sum, because remaining elements already have a sum equal to required sum.

Reference:

https://www.geeksforgeeks.org/partition-set-k-subsets-equal-sum/

698. Partition to K Equal Sum Subsets的更多相关文章

  1. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 698. Partition to K Equal Sum Subsets 数组分成和相同的k组

    [抄题]: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  3. [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether it's possible to divi ...

  4. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  5. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  6. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  7. 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 ...

  8. [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 ...

  9. HDU-3280 Equal Sum Partitions

    http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...

随机推荐

  1. 使用jmeter工具测试上传接口

    1.方法选择post:上传都是post上传. 2.路径输入正确的上传接口路径,并勾选Use multipart/form-data for POST 3.添加文件,文件路径尽量不要有中文,防止编码问题 ...

  2. css简单分页

    html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  3. db2 快照 SNAPSHOT

    打开和关闭快照缺省情况不打开 DB2 监控,必须在连接或实例级别上进行设置.有一系列监视器开关来决定是否监控某种数据元素.还预留了一个内存堆,用于包含为监控而存储的信息.1:在instance级别上设 ...

  4. 连接redis

  5. tomcat 时间相差8个小时,百度上查到的,备份下

    通常网上一查都是 修改 tomcat 的参数 ,如catalina 文件,jvm parameters 等.如果都不起作用,可以使用如下方式.. 你可以修改jdk的时间校正了,你这么来.进入 \hom ...

  6. shell的一些简单用法

    一 BASH的属性 BASH中会存储一些自身属性的参数,启用或关闭某一项功能 例如控制* .字符是否为通配 查看参数 set -o 关闭noglob参数 set -o noglob ls * ls: ...

  7. 2018.10.05 NOIP模拟 阶乘(简单数论)

    传送门 签到题. 直接把所有数先质因数分解. 同时统计每一个在阶乘中会出现的质数出现的最少次数. 然后对于每一个这样的质数,我们求出满足其出现质数的m的最小值,然后求出所有m的最大值. 求m的时候可以 ...

  8. 2018.06.29 NOIP模拟 排列(线段树)

    排列(premu.cpp) [题目描述] 对于一个 1 到 n 的排列,逆序数的定义为:排列中第 i 位 ai的逆序数就是 a1-ai-1中比 ai大的数的个数.另外用 pi表示 a1,-,ai的逆序 ...

  9. hdu-1140(求距离,精度判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1140 思路:卫星只能消灭地面上一部分的风暴,即风暴与卫星的距离最大是卫星到地球的切线的距离,大于这个距 ...

  10. 23. Man and His Natural Habitat 人类及其自然栖息地

    . Man and His Natural Habitat 人类及其自然栖息地 ① Ecology is that branch of science which concerns itself wi ...