原题链接在这里: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. 如何防止通过URL地址栏直接访问页面

    如何防止通过URL地址栏直接访问页面 一.解决方案 1,将所有页面放在WEB-INF目录下 WEB-INF是Java的web应用安全目录,只对服务端开放,对客户端是不可见的.所以我们可以把除首页(in ...

  2. FTP pure-ftpd 安装、管理

    FTP简介 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为文传协议,用户Internet上的控制文件的双向传输. FTP的主要作用,就是让用户链接上一个远 ...

  3. 深入解析Koa之核心原理

    这篇文章主要介绍了玩转Koa之核心原理分析,本文从封装创建应用程序函数.扩展res和req.中间件实现原理.异常处理的等这几个方面来介绍,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参 ...

  4. ElasticSearch高可用集群环境搭建和分片原理

    1.ES是如何实现分布式高并发全文检索 2.简单介绍ES分片Shards分片技术 3.为什么ES主分片对应的备分片不在同一台节点存放 4.索引的主分片定义好后为什么不能做修改 5.ES如何实现高可用容 ...

  5. ASP.NET动态生成GridView的使用

    根据DataTable动态生成包含checkbox的GridView,其中DataTable中对应checkbox那一列的值必须为bool值. public static GridView Dynam ...

  6. Apache顶级项目 Calcite使用介绍

    什么是Calcite Apache Calcite是一个动态数据管理框架,它具备很多典型数据库管理系统的功能,比如SQL解析.SQL校验.SQL查询优化.SQL生成以及数据连接查询等,但是又省略了一些 ...

  7. SEO提高网站排名快速见效的方法

    SEO如何提升网站排名?seo中文名称搜索引擎优化,是提升网站排名的一种技术手段,常用的手段就是优化标签,内链外链等等,最核心的优化方向,“关键词”.最常见的表现形式就是软文,但是有的时候就算你做了很 ...

  8. 简易的解决方式linker command failed with exit code 1 (use -v to see invocation)

    linker command failed with exit code 1 (use -v to see invocation) 遇到这个问题先不要慌,不用纠结是不是自己改动了什么代码导致的. 长话 ...

  9. socket编程 TCP 粘包和半包 的问题及解决办法

    一般在socket处理大数据量传输的时候会产生粘包和半包问题,有的时候tcp为了提高效率会缓冲N个包后再一起发出去,这个与缓存和网络有关系. 粘包 为x.5个包 半包 为0.5个包 由于网络原因 一次 ...

  10. linux sed使用(转)

    sed入门详解教程 sed 是一个比较古老的,功能十分强大的用于文本处理的流编辑器,加上正则表达式的支持,可以进行大量的复杂的文本编辑操作.sed 本身是一个非常复杂的工具,有专门的书籍讲解 sed ...