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. EasyMvc入门教程-高级控件说明(19)表单控件

    我们开发的系统,主要的一个功能就是采集界面数据,传回到服务器,比如:录入学生信息,这时候就需要表单. EasyMvc提供了如下界面控件,如下图所示:(控件+布局) EasyMvc实现了基于模型的绑定, ...

  2. EasyMvc入门教程-高级控件说明(20)表格控件

    表单与表格是信息化系统里很常见的控件,EasyMvc提供了简单的数据绑定方式(基于Json),看下面的示例:  准备的接口地址代码如下:(该接口适用以下所有例子) public IActionResu ...

  3. yii2操作数据库 mysql 读写分离 主从复制

    转载地址:http://www.kuitao8.com/20150115/3471.shtml 开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的&quo ...

  4. eclipse离线安装插件过程

    离线安装插件: 1. help -> install New Softe.. 2. 打开安装插件界面 最后点击,next, 同意事项,重启eclipse.

  5. DevExpress控件之TreeList

    基于v18.1 使用AppendNode方法手动赋值时,首先要添加treeListColumn 默认样式                                     修改后的样式   1 ...

  6. Ajax学习(一)——与Ajax的初次相识

        AJAX是"Asynchronous Javascript And XML"的缩写,从字面上解释是"异步JavaScript和XML"的简称. 它不是一 ...

  7. sublime添加sass编译

    首先安装Ruby环境sass是基于ruby的产物,因此在安装sass前需要先安装ruby,如果用命令方式编译Sass也是必须安装ruby的.命令行编译sass见!下载Ruby windows 安装包: ...

  8. 我眼中的Oracle Database Software 和 Oracle Database

    我眼中的Oracle Database Software 和 Oracle Database 我喜欢用微软的office软件和word文档(确切的说是:自己写的word文档,能够把这个Word文档想象 ...

  9. git 工具 - 子模块(submodule)

    From: https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97 子模块 有种情况我们经常 ...

  10. 图像检测之sift and surf---sift中的DOG图 surf hessian

    http://www.cnblogs.com/tornadomeet/archive/2012/08/17/2644903.html http://www.cnblogs.com/slysky/arc ...