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 ...
随机推荐
- 【笔记】H5自适应(待)
参考: 1.盒子模型:http://www.cnblogs.com/sunyunh/archive/2012/09/01/2666841.html 2.浮动:http://www.w3school.c ...
- Thrift简单调用
pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- Sybase数据库:两个特别注意的地方
Sybase数据库:两个特别注意的地方 一.字段别名 字段别名不能为查询条件中的列名,会导致查询出来的数据不准确:最好字段别名为非列名: 二.更新的表名的大小写 update a set .... s ...
- nagios报错Error: No such CGI app - /usr/local/nagios/sbin/nagios/cgi-bin/status.cgi may not exist or is not executable by this process.
加上rewrite rewrite ^/nagios/cgi-bin/(.*)\.cgi /$.cgi break;
- springmvc跨域
//mvc默认是text/plain;charset=ISO-8859-1@RequestMapping(value = "/xxx", produces = "appl ...
- GridView右键菜单
一.添加右键菜单 1.在VS工具箱中的“菜单和工具栏”找到ContextMenuStrip控件,双击添加. 2.点击ContextMenuStrip右上方的小三角形,打开编辑项,可以添加菜单项.至于菜 ...
- 技巧.【转】在虚拟机Vmware中使用HID设备(如USB免驱键盘)
ZC:我的环境:Win7x64.VMware10 ZC:我的处理: ZC: (1).usb.generic.allowHID = "TRUE" (本来就有,将它的位置提前) ZC: ...
- const 函数参数
void func(int value); 这样的函数,不可以这样子使用: const int value =100; func(value ); 因为func里面可能会对value进行更改,将con ...
- python 爬虫004-使用urllib2与正则表达式扒取糗事百科新鲜页首页帖子
面向过程的方式 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib2 import sys import re import os ...
- idea maven打包 install 报错The packaging for this project did not assign a file to the build artifact
如题,这其实是个低级错误,这个错的意思是,找不到这个插件的包. 原因很简单,不是找不到这个打包插件,而是自己的项目没有从maven仓库里加载这个包到项目里,因此会找不到. 看一下问什么会报这个错: 大 ...