Partition to K Equal Sum Subsets——LeetCode进阶路
原题链接https://leetcode.com/problems/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.
思路分析
给定一个整数数组,一个整数,返回是否数组能够被分为k个等值的子集。
直观的想,用递归,一个一个子集的凑,一旦有凑不上的则返回false
还有就是注意些特殊情况:
- 分组数大于数组的长度,返回false
- 若数组元素之和不能被数组数整除,则肯定不能满足分组,返回false
- 若分组数为1的话,直接返回数组本身就好,显然返回true
- 当有超过平均值的数组元素,返回false
notes : 一定记得回溯……
笔者小陌第N次忘记回溯(╯︵╰)
AC解
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
if(k == 1)
{
return true;
}
if(nums == null || nums.length == 0 || k > nums.length)
{
return false;
}
int sum = 0;
int max = 0;
for(int i:nums)
{
sum = sum + i ;
max = Math.max(max,i);
}
if(sum % k != 0 || sum / k < max)
{
return false;
}
boolean[] flag = new boolean[nums.length];
return dfs(nums,flag,k,sum /k,0,0);
}
public boolean dfs(int[] nums , boolean[] flag , int k , int subset , int curSubset , int position)
{
if(k == 0)
{
return true;
}
if(subset == curSubset)
{
return dfs(nums,flag,k-1,subset,0,0);
}
for(int i = position ; i<nums.length ; i++)
{
if(flag[i])
{
continue;
}
flag[i] = true;
if(dfs(nums,flag,k,subset,curSubset+nums[i],i+1))
{
return true;
}
flag[i] = false;//记得回溯!!!!
}
return false;
}
}
Partition to K Equal Sum Subsets——LeetCode进阶路的更多相关文章
- [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 ...
- 【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
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指定了 ...
- 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 ...
- 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] 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 ...
随机推荐
- 批量上传序列到Gisaid数据库
本文演示的是用Gisaid官网自带的Excel宏批量上传毒株信息和序列到Gisaid数据库,需要魔法. 1. 下载GISAID Batch Uploader 找到自己需要上传的病毒类型,我是Flu,在 ...
- mybatis - [09] 动态SQL
题记部分 一.if & test 如果id,name,age不为空,则按照指定的值进行查询.如果这三者都是空(null和空字符串),则该sql执行结果为全表查询的结果集. <select ...
- docker - [14] redis集群部署
本章节是在一个服务器上进行演示 一.准备工作 (1)创建redis集群使用的网络:redis-net docker network create redis-net --subnet 172.38.0 ...
- 如何通过 Linux Bash 技术,让你的独立产品实现一键安装
我在业余时间开发了一款自己的独立产品:升讯威在线客服与营销系统.陆陆续续开发了几年,从一开始的偶有用户尝试,到如今线上环境和私有化部署均有了越来越多的稳定用户. 虽然我编写了非常详细的在线文档,说明如 ...
- 面试题55 - I. 二叉树的深度
地址:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/ <?php /** 面试题55 - I. 二叉树的深度 输入一棵二 ...
- win7系统安装mysql新建数据库/数据表及故障处理,安装mysql后net start mysql服务无法启动
问题描述:win7系统安装mysql,安装mysql后net start mysql服务无法启动 1.下载mysql: 官网地址:https://dev.mysql.com/downloads/mys ...
- 【数值计算方法】线性方程组迭代算法的Python实现
线性方程组迭代算法的Python实现 jacobi,GS,SOR迭代法 def JacobiIter(A:np.ndarray, b:np.ndarray, tol:float=1e-5, maxIt ...
- word 批量制作ppt
将 Word 文档作为大纲,构建演示文稿 在 Word 文档中,单击"开始". 使用"样式"设置 Word 文档中内容的格式. 突出显示要用作幻灯片标题的内容, ...
- ModuleNotFoundError: No module named '_ctypes' when Python3
前言 运行 python 报错:ModuleNotFoundError: No module named '_ctypes' when using Value from module multipro ...
- ATT&CK实战系列(三)红日靶场3
本次打靶练习是一个黑盒测试.没有密码,我们的目标是拿到域控制器的权限,并找到其中的重要文件. 网络拓扑 网卡设置 centos为出网机,第一次运行,需重新获取桥接模式网卡ip. 需重启网络服务serv ...