698. Partition to K Equal Sum Subsets
Given an array of integers
numsand a positive integerk, find whether it's possible to divide this array intoknon-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 ...
随机推荐
- struts框架中OGNL表达式的使用之jsp页面获取action中的属性值
在jsp页面中获取action中的值: 1.写一个action类OgnlAction类: 需要注意的地方: 如果在aciton中直接使用ognl表达式,将值存储的值栈中,是不能通过跳转将值传到jsp页 ...
- python激活码
- [BAT]批处理脚本双击可运行,但在定时计划任务中无法执行(当前工作路径不对导致的)
一开始,红色部分我是用set AutoPath=%cd%,双击可执行,但是将这个批处理脚本放在定时任务中无法执行,后来发现在定时执行的时候,当前工作路径不是批处理脚本所在的路径,而是C:/Window ...
- Java数据结构和算法(五)二叉排序树(BST)
Java数据结构和算法(五)二叉排序树(BST) 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 二叉排序树(Binary S ...
- 重新学习pytorch的库函数等..
http://blog.csdn.net/victoriaw/article/list/10 开始第一篇: http://blog.csdn.net/VictoriaW/article/details ...
- div浮停在页面最上或最下
div{ position:fixed; bottom:0px; // top:0px; z-index:999; } bottom:0px 浮停在最下面,top:0px 浮停在最上面:z-index ...
- const变量指针赋值给非const类型的指针运行结果
在c++可以定义一个const变量,然后把变量的值赋给一个非const指针,可以通过指针来改变const变量的值吗?下面的截图给出了答案
- Ubuntu 16.04安装MySQL及遇到的问题解决方案
使用以下命令即可进行MySQL安装: sudo apt-get install mysql-server 上述命令会安装以下包: apparmor mysql-client-5.7 mysql-com ...
- Python调用Google翻译
出自:http://blog.csdn.net/zhaoyl03/article/details/8830806 最近想动手做一个文档自动下载器,需要模拟浏览器的行为.虽然感觉思路上没有困难,但在技术 ...
- 使用系统的CoreLocation定位
//// ViewController.m// LBS//// Created by tonnyhuang on 15/8/28.// Copyright (c) 2015年 tonnyhua ...