LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/
题目:
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.
题解:
首先计算sum, 看sum能否被k整除. 若不能, 铁定不能分成k组. return false.
若能的话,每组的target sum就该是sum/k. 一组一组的减掉. 直到 k = 1. 剩下最后一组, 最后一组的sum肯定是sum/k.
因为这里的已经验证过sum是k的倍数, 而前面已经有k-1组 sum/k找到了. 所以可以直接return true.
This is bottom-up recursion. Set parameters for state first.
It needs count to count number in subarray. Since there may be negative number in nums. If target is 0, there could be [-1, 1] or empty subarray.
The reason state has both visited and cur starting index is because of trimming dfs tree.
When summing up to target, if index i can't be used, when trying j > i, the next level of DFS, there is no need to try i again. Because if i works, it would be added into res before.
The only case i could be used is to sum up next target.
Note: the question is asking for non-empty, we need to add a count of each sub set. And make sure it is > 0 before accumlating to result.
Time Complexity: exponential.
Space: O(n). stack space.
AC Java:
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
if(nums == null || nums.length == 0){
return false;
}
int sum = 0;
for(int num : nums){
sum += num;
}
if(sum % k != 0){
return false;
}
boolean [] visited = new boolean[nums.length];
return dfs(nums, visited, 0, 0, sum/k, 0, k);
}
private boolean dfs(int [] nums, boolean [] visited, int cur, int sum, int target, int count, int k){
if(sum > target){
return false;
}
if(k == 1){
return true;
}
if(sum == target && count > 0){
return dfs(nums, visited, 0, 0, target, 0, k-1);
}
for(int i = cur; i<nums.length; i++){
if(!visited[i]){
visited[i] = true;
if(dfs(nums, visited, i+1, sum+nums[i], target, count++, k)){
return true;
}
visited[i] = false;
}
}
return false;
}
}
类似Partition Equal Subset Sum, Matchsticks to Square.
LeetCode Partition to K Equal Sum Subsets的更多相关文章
- [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】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [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指定了 ...
- 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 ...
- 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 ...
- 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 ...
- [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 fol ...
随机推荐
- 最牛技术 1秒启动Linux的窍门
1秒启动Linux可以实现吗?我们知道Linux系统开机并不算快,最少也需要11秒,但是,现在有一个技巧,可以1秒打开linux系统,到底是什么技术这么牛?请看下文详细介绍 尽可能快的启动系统,对于自 ...
- quartz(7)-源码分析
定时器启动 上图通过spring加载quartz <bean id="scheduler" class="org.springframework.schedulin ...
- web.xml上监听器作用
<!--Spring ApplicationContext 载入 --> <listener> <listener-class>org.springframewor ...
- jmeter-请求参数化
新建个scv文件,将我们需要传递的数据写进去(建议用notepad等编辑器,直接用excel转csv格式有可能会出现不能识别参数) 有多个参数用,分开 另存为 2.jmeter 新建请求,选择函数对话 ...
- 支付宝app支付java后台流程、原理分析(含nei wang chuan tou)
java版支付宝app支付流程及原理分析 本实例是基于springmvc框架编写 一.流程步骤 1.执行流程 当手机端app(就是你公司开发的app)在支付 ...
- python递归列出目录及其子目录下所有文件
python递归列出目录及其子目录下所有文件 一.前言 函数的递归,简单来说,就是函数内部调用自己 先举个小例子,求阶乘 def factorial(n): if n == 0: return 1 e ...
- 利用$http获取在服务器的json数据
以下是存储在web服务器上的 JSON 文件: http://www.runoob.com/try/angularjs/data/Customers_JSON.php { "records& ...
- Django进阶Model篇004 - ORM常用操作
一.增加 create和save方法 实例: 1.增加一条作者记录 >>> from hello.models import * >>> Author.object ...
- 不同局域网如何利用charles对app进行抓包
晚上遇到了一种尴尬的场景:电脑并没有无线网卡,电脑是通过有线连接的,但是手机连的是公共的wifi,二者并不在同一个网段 在试过很多办法无解后,终于百度出一种"曲线救国"的办法(以荣 ...
- MongoDB架构——记得结合前面的文章看,里面的图画的很好
转自:http://www.ha97.com/4580.html 本文图片来自Ricky Ho的博文MongoDB构架(MongoDB Architecture),这是个一听就感觉很宽泛的话题,但是作 ...