698. Partition to K Equal Sum Subsets
Given an array of integers
nums
and a positive integerk
, find whether it's possible to divide this array intok
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的更多相关文章
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 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 ...
- [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 ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- [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 ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- 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 ...
- [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 ...
- HDU-3280 Equal Sum Partitions
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...
随机推荐
- 设置 svn 与 web线上同步
默认你已经配置好了svn服务 1.假设我们的线上网站目录为:/data/www/xxx 2.假设svn的仓库目录为:/data/svn/repo 一.checkout一份svn到线上网站目录 svn ...
- Spring框架的IOC之注解方式的快速入门
1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring框架的AOP的jar包,spring-aop的jar包 2. 步骤二:创建对应的包结构, ...
- struts框架问题五之向值栈中保存数据
5. 问题五: 向值栈保存数据 (主要针对root栈) > valueStack.push(Object obj); * push方法的底层调用root对象的push方法(把元素添加到0位置) ...
- python while语句写法
count=5 while count>0: print 'i love python' count=count-1 else: print 'over'
- nfs文件系统在linux下挂载不上的解决办法
标签: mount nfs export 2014年08月07日 18:46:247472人阅读 评论(0) 收藏 举报 分类: 文件系统(10) 版权声明:本文为博主原创文章,未经博主允许 ...
- vue.js项目打包上线
最近一直坚持每个月写一个小的vue.js 开发的项目,最后开发完成后想到很久之前给别人回答的一个问题:vue的项目如何上线,当时有千奇百怪的回答,我在想,这些人都是肿了么,vue的官方都说了,这个框架 ...
- 原型工具之团队协作: Axure VS Mockplus
一款软件产品的诞生,必然会经历一个过程:需求分析.设计.开发.测试.上线.如此反复迭代.而设计阶段中,原型设计.制作.交流.审核.迭代,是软件设计和开发的重要保障. 简单的一段话,我们可以解读出很多隐 ...
- 1、HttpClient初探
HttpClient是它的核心接口.可以理解为一个简单的浏览器. 主要方法有: getParams(); 获取运行参数 getConnectionManager(); 获取连接管理器.连接管理器中 ...
- css长度
在CSS样式表中,长度单位分两种: 相对长度单位,如px, em等绝对长度单位,如pt,mm等 CSS相对长度单位(relative length unit) CSS相对长度单位中的相对二字,表明了其 ...
- 如何修改路由器的登录IP地址?
如何修改路由器的登录IP地址? 因为有多个路由器,为了区分不同路由器,我们可以修改它的登录IP,而且修改后,可以在连接的电脑上直观地知道所连接的是哪一台路由器 买回来的路由器,一般默认的登录地址是19 ...