leetcode 698. Partition to K Equal Sum Subsets
来源:互联网 发布:大数据进阶 编辑:程序博客网 时间:2024/06/13 08:06
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 array into k
non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4Output: TrueExplanation: 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
.class Solution {public: bool canPartitionKSubsets(vector<int>& nums, int k) { //先要知道sum是多少 sum = 0; for (auto it : nums) sum += it; if (sum % k != 0) return false; sum = sum / k; sort(nums.begin(), nums.end(), greater<int>()); for (int i = 0; i < k; i++) group.push_back(0); return helper(nums, 0, group, sum); } bool helper(vector<int>& nums, int i, //当前需要安排第i个 vector<int> group, int all) //每一组的和 { if (i == nums.size()) return true; for (int j = 0; j < group.size(); j++) { if (group[j] + nums[i] <= all) //可以往里面放 { group[j] += nums[i]; if (helper(nums, i + 1, group, all)) return true; group[j] -= nums[i]; } } return false; } private: int sum; vector<int> group;};
阅读全文
0 0
- Leetcode | Partition to K Equal Sum Subsets
- [LeetCode] Partition to K Equal Sum Subsets
- leetcode 698. Partition to K Equal Sum Subsets
- Leetcode: 698. Partition to K Equal Sum Subsets
- leetcode-698. Partition to K Equal Sum Subsets
- leetcode 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets
- leetcode-698-Partition to K Equal Sum Subsets
- LeetCode #698 Partition to K Equal Sum Subsets
- Partition to K Equal Sum Subsets
- LWC 54:698. Partition to K Equal Sum Subsets
- 698. Partition to K Equal Sum Subsets(M)
- jquery选择器
- OpenCV拷贝与ROI
- 卷积神经网络(Convolutional Neural Network)
- AndroidStudio(3.0.x版本)输入法无提示问题解决方案
- invalid mode 报错
- leetcode 698. Partition to K Equal Sum Subsets
- 消息机制——MQ
- 电脑无线网络无缘无故自动关闭,适配器正常,网络正常却怎么连都连不上?
- FPGA中韩彬(大神)的异步复位,同步释放方法
- 【代码笔记】【c#】将数组的某一索引位置的元素移动到指定索引位置
- MOV指令、LDR指令、LDR伪指令之间的区别
- 20171122
- Openssl CA证书生成以及双向认证,及windows系统证书批量导出,android cer转bks
- linux 大文件和大目录查找