原题链接在这里: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. 请求库之requests,selenium

    requests模块 一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...

  2. windows vs 编译tesseract-ocr

    第一把所有相关的代码都下载,github最方便 https://github.com/tesseract-ocr/tesseract 要点1.cppan c++的中文管理包,很方便,需要翻-墙,安装包 ...

  3. springboot学习(三)——使用HttpMessageConverter进行http序列化和反序列化

    以下内容,如有问题,烦请指出,谢谢! 对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序 ...

  4. JSR规范整理

    Web Service技术 Java Date与Time API ( JSR 310) Java API for RESTful Web Services (JAX-RS) 1.1 (JSR 311) ...

  5. 低版本C++ string的万能转换,从long string 之间的转换来看看

    string 转 long 那必须是万年atoi(),不过得配合c_str()使用! [plain] view plain copy #include <string> #include  ...

  6. Elasticsearch 配置优化

    cluster.routing.allocation.same_shard.host:true 这会防止同一个shard的主副本存在同一个物理机上(因为如果存在一个机器上,副本的高可用性就没有了). ...

  7. 使用nagios检测windows服务器

    1.安装nagios windows客户端 下载NSClient++的安装包,注意根据实际的32/64CPU来下载 下载地址 http://sourceforge.net/projects/nscpl ...

  8. 关于angularJS的$watch的 一些小用法

    $watch方法,它可以帮助我们在每个scope中监视其中的变量. $watch 单一的变量 对于普通的变量时,如数字,字符串等,直接如下写是可以监视到变量的变化,并执行相应的函数的. $scope. ...

  9. 关于JDK1.8 HashMap扩容部分源码分析

    今天回顾hashmap源码的时候发现一个很有意思的地方,那就是jdk1.8在hashmap扩容上面的优化. 首先大家可能都知道,1.8比1.7多出了一个红黑树化的操作,当然在扩容的时候也要对红黑树进行 ...

  10. JSON Web Token (JWT) 简介

    JSON Web Token (JWT) 是一种基于 token 的认证方案. JSON Web Token 的结构 一个 JWT token 看起来是这样的: eyJhbGciOiJIUzI1NiI ...