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 ...
随机推荐
- MessagePack Java 0.6.X List, Map 对象的序列化和反序列化
为了序列化原生的容器对象例如 List 和 Map 对象,你必须使用 Template. Template 对象是 serializer 和 deserializer 的配对.例如,为了序列化一个 ...
- 存储映射--mmap
存储映射 使一个磁盘文件与存储空间中的一个缓冲区相映射. 当从缓冲区中取数据,就相当于读文件中的相应字节. 将数据存入缓冲区,则相应的字节就自动写入文件. 使用这种方法,首先应通知内核,将一个指定文件 ...
- MySQL基础入门之常用命令使用
如何启动MySql服务 /etc/init.d/mysqld start service mysqld start Centos .x 系统 sysctl start mysqld 检测端口是否运行 ...
- [CSP-S模拟测试]:数学课(找规律+数学)
题目传送门(内部题145) 输入格式 从$math.in$读入数据. 第一行两个数,为$n,q$.接下来$q$行每行一个数$m$,询问大小为$m$的$A$一共有多少个. 输出格式 输出答案到$math ...
- 如何使用Navicat监控mysql数据库服务器
https://jingyan.baidu.com/article/c33e3f48de5208ea15cbb525.html 打开Navicat 点击[工具]菜单,选择[服务器监控]下的[MyS ...
- Python中循环及判断语句
循环判断条件是编程语言中一个很重要的部分,python也不例外,循环判断条件一般结合continue,return,break关键字来判断,这些关键字用法与java中基本一致 一.if判断语句 判断条 ...
- venv转向pipenv
先编译安装你需要的Python版本:参考https://www.cnblogs.com/zxpo/p/10011871.html python3.6安装在:/usr/bin/python3.6目录下: ...
- 二、WebSphere Application Server上部署war包并访问
进入我们was服务器控制台之后我们直接按照下图操作: 2.选择要上传的war包,下一步 3.一直下一步,步骤4注意填好“上下文根”,然后继续下一步,直到完成. 4.点击保存到主配置 5.应用程序> ...
- re 模块, 正则表达式 \w+\d+ 的重复问题引发的题目解析
题目 计算以下代码的结果 s = "?!.18)dajslj$12.15613sdadw.123sdasda35615.168sndsda$15.6sdasd.sdfsdgw123.156s ...
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...