原题链接在这里: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 SumMatchsticks to Square.

LeetCode Partition to K Equal Sum Subsets的更多相关文章

  1. [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 ...

  2. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  3. [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 ...

  4. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. [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. 自动回复之实现随机回复与常用Mapper XML标签

    [常用Mapper XML标签] 1.基本的:select.insert.update 等 2.可读性.方便拼SQL:where.set.trim 3.减少重复:sql 4.逻辑控制:if.choos ...

  2. 纯CSS3滑动开关按钮

    在线演示 本地下载

  3. ping主机脚本

    #!/bin/bash #ping net='172.16.1' uphosts=0 downhosts=0 for i in {1..254};do ping -c 1 -w 1 ${net}.${ ...

  4. 记一次网卡报错ERROR,some other host already uses address

    提示IP地址冲突,但是此IP确实没有被其他Server占用 解决如下: 编辑此文件 搜索arping 将下面几行注释掉 保存退出 激活网卡 此时IP地址已生效 下面是我的系统版本 (一般应该不会出现这 ...

  5. Deep Auto-encoder

    autoencoder可以用于数据压缩.降维,预训练神经网络,生成数据等等. autoencoder的架构 autoencoder的架构是这样的: 需要分别训练一个Encoder和一个Decoder. ...

  6. HTTP响应代码集合

    用于表示临时响应并需要请求者执行操作才能继续的状态代码.代码说明100(继续)请求者应当继续提出请求.服务器返回此代码则意味着,服务器已收到了请求的第一部分,现正在等待接收其余部分. 101(切换协议 ...

  7. OwinStartup not firing

    https://stackoverflow.com/questions/20203982/owinstartup-not-firing 缺少依赖 Make sure you have installe ...

  8. redhat6.4 数据包无法到达

    由于redhat在初始化的时候,防火墙设置为icmp-host-prohibited,导致数据包无法到达. 具体iptables(所在目录/etc/sysconfig)如下: # Firewall c ...

  9. [ SSH 两种验证方式原理 ]

    SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会把自己的公钥发送给用户: (2) 用户会根据服务器发来的公钥对密码进行加密: (3) 加密后的信 ...

  10. mysql数据库优化课程---11、mysql普通多表查询

    mysql数据库优化课程---11.mysql普通多表查询 一.总结 一句话总结:select user.username,user.age,class.name,class.ctime from u ...