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 = 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的更多相关文章
- [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 ...
- 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 ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 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] 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 ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- [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 ...
- HDU-3280 Equal Sum Partitions
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...
随机推荐
- 【Python之路】特别篇--Git & GitHub
版本控制工具主要实现2个功能: 版本管理 在开发中,这是刚需,必须允许可以很容易对产品的版本进行任意回滚,版本控制工具实现这个功能的原理简单来讲,就是你每修改一次代码,它就帮你做一次快照 协作开发 一 ...
- 如何将vs2015中的英文注释改为中文
找一个中文版的vs 将 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework 文件夹 ...
- 27.Python列表(list)、元组(tuple)、字典(dict)和集合(set)详解
本章将会介绍 Python 内置的四种常用数据结构:列表(list).元组(tuple).字典(dict)以及集合(set). 这四种数据结构一但都可用于保存多个数据项,这对于编程而言是非常重要的,因 ...
- [CSP-S模拟测试]:密码(数位DP+库默尔定理)
题目描述 为了揭穿$SERN$的阴谋,$Itaru$黑进了$SERN$的网络系统.然而,想要完全控制$SERN$,还需要知道管理员密码.$Itaru$从截获的信息中发现,$SERN$的管理员密码是两个 ...
- Linux 服务器安装jdk,mysql,tomcat简要教程
linux服务器是阿里云上买的,学生价9.9/月,拿来学习下. 需要准备软件工具: 1.editplus (编辑服务器上的文件) 2.PuTTY (Linux命令连接器) 3.FlashFXP(上传文 ...
- IDEA:Process finished with exit code -1073741819 (0xC0000005)
出门左转:https://www.cnblogs.com/virgosnail/p/10335224.html
- SpringBoot2.X&Prometheus使用
1.启动Prometheus [root@ip101 prometheus-2.12.0.linux-amd64]# pwd /opt/app/prometheus-2.12.0.linux-amd6 ...
- 黑马vue---10-11、Vue实现跑马灯效果
黑马vue---10-11.Vue实现跑马灯效果 一.总结 一句话总结: 1. 给 [浪起来] 按钮,绑定一个点击事件 v-on @ 2. 在按钮的事件处理函数中,写相关的业务逻辑代码:拿到 ...
- windows上配置pytorch
操作系统:win10 已安装程序:Python 3.6 + Anaconda 5.1.0 + CUDA 9 pytorch官网:https://pytorch.org/ 1.进入官网,从Get Sta ...
- java源码-Semaphore源码分析
Semaphore内部Sync对象同样也是继承AQS,跟Reentrant一样有公平锁跟非公平锁区分,但是Semaphore是基于共享锁开发,Reentrant是基于独占锁开发.主要就是初始化Sema ...