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
.
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum = ;
for (int num : nums) sum += num;
if (k <= || sum % k != ) return false;
int[] visited = new int[nums.length];
return canPartition(nums, visited, , k, , , sum / k);
} public boolean canPartition(int[] nums, int[] visited, int start_index, int k, int cur_sum, int cur_num, int target) {
if (k == ) return true;
if (cur_sum > target) return false;
if (cur_sum == target && cur_num > 0) return canPartition(nums, visited, , k - , , , target);
for (int i = start_index; i < nums.length; i++) {
if (visited[i] == ) {
visited[i] = ;
if (canPartition(nums, visited, i + , k, cur_sum + nums[i], cur_num++, target)) {
return true;
}
visited[i] = ;
}
}
return false;
}
}
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 ...
- 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 ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- 【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指定了 ...
- [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 ...
随机推荐
- removeAttr(name)
removeAttr(name) 概述 从每一个匹配的元素中删除一个属性 1.6以下版本在IE6使用JQuery的removeAttr方法删除disabled是无效的.解决的方法就是使用$(" ...
- hdu 5738 Eureka 极角排序+组合数学
Eureka Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 灰度图像--图像分割 阈值处理之P-Tile阈值
学习DIP第53天 转载请标明本文出处:***http://blog.csdn.net/tonyshengtan ***,出于尊重文章作者的劳动,转载请标明出处!文章代码已托管,欢迎共同开发:http ...
- bzoj4321
queue2 HYSBZ - 4321 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两 人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在多少方 ...
- EasySwoole 在mac上装虚拟机centos共享mac目录报错处理
[2019-09-26 21:37:09][DEBUG][ERROR] : [listen /media/psf/Home/wwwroot/myobj/easyswoole/Temp/TaskWork ...
- Java基础__随机生成1~15之间不重复的数字
package text; import java.util.ArrayList; import java.util.List; public class Text { public static v ...
- Xargs用法详解(自创)
简介之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: 这个命令是错误的find /sbin -perm +700 |ls -l ...
- 7.2 Mybatis
7.2 Mybatis 平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类. 不仅如此,访问 ...
- flask第二篇 三剑客+特殊返回值
1.Flask中的HTTPResponse 在Flask 中的HttpResponse 在我们看来其实就是直接返回字符串 2.Flask中的Redirect 每当访问"/redi" ...
- LC 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...