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 ...
随机推荐
- 【Flask】Flask-Migrate基本使用
# flask_migrate笔记:在实际的开发环境中,经常会发生数据库修改的行为.一般我们修改数据库不会直接手动的去修改,而是去修改ORM对应的模型,然后再把模型映射到数据库中.这时候如果有一个工具 ...
- ssi include返回404页面
项目中index.html中包含<!--#include virtual="/commonfrag/djdzkan/recomm_www_info.inc" --> ...
- Mahout 分类算法
实验简介 本次课程学习了Mahout 的 Bayes 分类算法. 一.实验环境说明 1. 环境登录 无需密码自动登录,系统用户名 shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu ...
- 20145109 《Java程序设计》第四周学习总结
20145109 <Java程序设计>第四周学习总结 教材学习内容总结 Chapter 6 Inheritance & Polymorphism What is Inheritan ...
- Flume-NG源码阅读之AvroSink
org.apache.flume.sink.AvroSink是用来通过网络来传输数据的,可以将event发送到RPC服务器(比如AvroSource),使用AvroSink和AvroSource可以组 ...
- mysql数据库优化课程---11、mysql普通多表查询
mysql数据库优化课程---11.mysql普通多表查询 一.总结 一句话总结:select user.username,user.age,class.name,class.ctime from u ...
- php将科学计算法得出的结果转换成原始数据
由于php最大只支持显示 15位因的数据运算,大于15位的2数加减乘除的数据的结果,会直接用科学计数法显示, 但在现实生活中,科学计数法不利于普通人识别,所以,本函数将:科学计数法的出的结果转换成原始 ...
- python脚本3_输入若干个整数打印出最大值
#输入若干个整数,打印出最大值 # m = int(input('Input first number >>>')) while True: c = input('Input a n ...
- 在阿里云上安装python3.4和pycharm
一. 安装python3.4 二. 安装pycharm 三. 安装可视化界面和远程桌面连接 四. 启动和配置pycharm 五. 安装更多字体 六. 给pycharm设置桌面快捷方式 一. 安装pyt ...
- poj3683 2 -sat输出路径
tarjan缩点,拓扑排序染色输出(貌似挑战上面没有拓扑啊,而且这样写还过了= =) 主要是找s,t,d,三者之间的关系,找出合取范式这题就很容易了 #include<map> #incl ...