Problem

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.

Solution

class Solution { public boolean canPartitionKSubsets(int[] nums, int k) { int sum = 0; for (int num: nums) sum += num; if (k == 0 || sum%k != 0 || sum &lt; k) return false; int target = sum/k; return dfs(nums, new boolean[nums.length], 0, k, 0, target); } private boolean dfs(int[] nums, boolean[] used, int start, int k, int sum, int target) { if (k == 1) return true; if (sum == target) return dfs(nums, used, 0, k-1, 0, target); for (int i = start; i &lt; nums.length; i++) { if (!used[i]) { used[i] = true; if (dfs(nums, used, i+1, k, sum+nums[i], target)) return true; used[i] = false; } } return false; } }
原文地址:https://segmentfault.com/a/1190000017013991

[LeetCode] 698. Partition to K Equal Sum Subsets的更多相关文章

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

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

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

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

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

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

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

  6. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  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 548. 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. openssl之EVP系列之12---EVP_Seal系列函数介绍

    openssl之EVP系列之12---EVP_Seal系列函数介绍     ---依据openssl doc/crypto/EVP_SealInit.pod翻译和自己的理解写成     (作者:Dra ...

  2. 危急,不要任意让站点记住password自己主动登陆!

    为了方便用户登录,差点儿全部的站点都实现了"记住password"."自己主动登陆"这样似乎人性化的功能. 我也非常喜欢这个功能,由于我自己的脑子实在是讨厌记东 ...

  3. Oracle数据泵远程导入文件到本地数据库

    --以dba身份登录 C:\Users\Administrator>sqlplus / as sysdba --创建用户 SQL> create user bfzg0828 identif ...

  4. Jenkins安装war版本

    Jenkins的war包安装很简单: 下载jenkins的war包地址:https://jenkins.io/download/ 选择对应的版本 然后放入tomcat启动就好,其他根据提示来就好,比较 ...

  5. linux中判断符号[]注意事项

    1.中括号[]内的每个组件都需要有空格键来分割: 2.在中括号内的变量,最好都一双引号括号起来: 3.在中括号内的常量,最好都以单引号或双引号括号起来.

  6. JAVA基础针对自己薄弱环节总结01(循环之前的知识)

    java中的标识符 组成:数字.字母.下划线.美元$符号组成.  规则:不能由数字开头.  类名:每一个单词的首字母大写  包名:所有小写  变量名.方法名:第一个单词首字母小写.后面首字母大写  常 ...

  7. 【Python】存储数据

    很多程序都要求用户输入某种信息,如让用户存储游戏首选项或者提供可视化数据,不管专注什么,程序都要将数据进行存储,那么如何存储呢? JSON(JavaScript Object Notation)格式最 ...

  8. Javascript模式(一) 单例模式

    function A(){ // 存储实例对象 var instance; // 重写构造函数,只返回闭包内的局部变量instance A = function(){ return instance; ...

  9. [转]Linux shell中的那些小把戏

    我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧.是的,我能记住一些命令,但是肯定不会只在特定的任务上使用一次,所以我就开始在我的Dropbox账号里用文本 ...

  10. Redis学习手册(List数据类型)(转)

    一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的 元素.在插入时,如果该键并不存在,Redi ...