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.

分析:

Assume sum is the sum of nums[] . The dfs process is to find a subset of nums[] which sum equals to sum/k. We use an array visited[] to record which element in nums[] is used. Each time when we get a cur_sum = sum/k, we will start from position 0 in nums[] to look up the elements that are not used yet and find another cur_sum = sum/k.

 class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum = ;
for (int num : nums) sum += num;
if (k <= || sum % k != ) return false;
int[] visited = new int[nums.length];
return canPartition(nums, visited, , k, , , sum / k);
} public boolean canPartition(int[] nums, int[] visited, int start_index, int k, int cur_sum, int cur_num, int target) {
if (k == ) return true;
if (cur_sum > target) return false;
if (cur_sum == target && cur_num > 0) return canPartition(nums, visited, , k - , , , target);
for (int i = start_index; i < nums.length; i++) {
if (visited[i] == ) {
visited[i] = ;
if (canPartition(nums, visited, i + , k, cur_sum + nums[i], cur_num++, target)) {
return true;
}
visited[i] = ;
}
}
return false;
}
}

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

  3. LeetCode Partition to K Equal Sum Subsets

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

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

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

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

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

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

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

  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. HDU-3280 Equal Sum Partitions

    http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...

随机推荐

  1. mysql优化(上)

    磁盘组成  和  磁盘读取过程 尽量减少 i/o 操作. 表结构设计:(1)三范式 :   原子性(不可拆分).唯一性(不能有完全相同的数据).无冗余性(不能有多余的数据),对于冗余性说明一下:拿订单 ...

  2. isset和empty以及is_null区别

    2.empty,isset首先都会检查变量是否存在,然后对变量值进行检测.而is_null 和 “参数本身”只是直接检查变量值,是否为null,因此如果变量未定义就会出现错误! 3.isset():仅 ...

  3. Jmeter接口测试之用例数据分离

    之前我们的用例数据都是配置在 Jmeter Http 请求中,每次需要增加,修改用例都需要打开 jmeter 重新编辑,当用例越来越多的时候,用例维护起来就越来越麻烦,有没有好的方法来解决这种情况呢? ...

  4. luogu 3441 [POI2006]MET-Subway 拓扑排序+思维

    Description 给出一棵N个结点的树,选择L条路径,覆盖这些路径上的结点,使得被覆盖到的结点数最多. Input 第一行两个正整数N.L(2 <= N <= 1,000,000, ...

  5. 顺序表应用8:最大子段和之动态规划法(SDUT 3665)

    Problem Description 给定n(1<=n<=100000)个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a ...

  6. vue刷新子页面,跳到主页,params传参引起的血案!

    今天,算是真正认识了params传参,为什么说params传参引起了血案? 起因是这样的,我正在做一个登陆的模块,公司想根据url不同的参数来区分是什么类型的会议, 于是后端推荐我用params传参的 ...

  7. 微信小程序搭建mpvue+vant+flyio

    导语 上一篇文章微信小程序搭建mpvue+vant已经介绍了如何搭起mpvue项目及引入vant,本篇文章继续在它的基础上,引入flyio,并做一些封装,目的是为了在小程序发起请求. 这时读者会有些疑 ...

  8. hive连接hbase

    使用hive连接hbase 前提说明:一个hive表指向一个hbase表,一对一,不能多对一 建立外部表 CREATE EXTERNAL TABLE test_hbase( key string, m ...

  9. Hive使用与安装步骤

    1.Hive安装与配置 Hive官网:https://hive.apache.org/ 1. 安装文件下载 从Apache官网下载安装文件 http://mirror.bit.edu.cn/apach ...

  10. Django的JWT机制工作流程

    https://blog.csdn.net/bin_1022/article/details/81278513 django-rest-framework-jwt token 怎么解码得到用户名? d ...