[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 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 < 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 < 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的更多相关文章
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 关闭火狐定期向“http://detectportal.firefox.com/”发包
问题:最近发现火狐总是向http://detectportal.firefox.com/发包 办法:firefox地址栏输入 about:config,然后搜索找到 network.captive-p ...
- SQLite创建表并加入数据
- (void)viewDidLoad { [super viewDidLoad]; //创建表 [self creatTable]; //插入数据 [self insertTable]; } // ...
- vue2.0 watch 详解
vue官网解释: 一个对象,键是需要观察的表达式,值是对应回调函数.值也可以是方法名,或者包含选项的对象.Vue 实例将会在实例化时调用 $watch(),遍历 watch 对象的每一个属性. 也就是 ...
- qs库使用
1.npm地址 https://www.npmjs.com/package/qs 2.概述 将url中的参数转为对象: 将对象转为url参数形式 3.示例 import qs from 'qs'; c ...
- Java中使用com.sun相关jar包出现编译错误,但是运行没有错误的解决方法和原因
[解决方法]如果你用的是Eclipse 在preference->java->complier->errors/warning->deprecated and restrict ...
- Linux 编译ffmpeg 生成ffplay
本来主要介绍linux环境下如何编译ffmpeg使之生成ffplay.编译总是离不开源码的版本,以及编译环境下:编译环境Ubutun 16.04 ,ffmpeg 版本3.4.2.如何下载ffmpeg ...
- caffe2--------ImportError: No module named past.builtins
whale@sea:~/anaconda2/lib/python2.7/site-packages$ python Python 2.7.14 |Anaconda custom (64-bit)| ( ...
- 还需要学习的十二种CSS选择器
在前面的文章中,我们在介绍了<五种你必须彻底了解的CSS选择器>,现在向大家介绍,还需要学习的另外十二种CSS选择器.如果你还没有用过,就好好学习一下,如果你已经熟知了就当是温习. 一.X ...
- Xenomai 3 migration
Xenomai 3 的rtdm驱动更像一般的Linux驱动,named device会在/dev/rtdm/xxx创建一个设备文件.而用户空间使用时,写得来也和Linux的一般char设备相似,ope ...
- 认识 service worker
离线缓存可以提升用户体验,可以节省网络资源,但是,浏览器对资源缓存和自定义网络请求的控制一直不够完善,service worker 的出现就是为了解决这些问题 它可以解决目前离线应用的问题,同时也可以 ...