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 ...
随机推荐
- docker - [08] Portainer可视化面板安装
Docker图形化界面管理工具 一.运行容器 同时下载和使用镜像运行容器 docker run -d -p 8088:9000 \ --restart=always -v /var/run/docke ...
- 代码托管平台对比分析:Gitee与GitLab
一.Gitee:本土化服务的深度实践者 Gitee凭借对中国开发者需求的精准洞察,提供了多项针对性优化功能,尤其适合国内团队: 高速稳定的访问体验 服务器均部署于国内,代码拉取.推送及CI/CD流程的 ...
- STM32实战——ESP8266 WIFI模块
ESP8266 硬件介绍 ESP8266系列模组有哪些: 在本实验中,ESP8266与ESP-01不做区分. ESP-01引脚介绍: 引脚 功能 3.3 3.3V供电,避免使用5V供电 RX UART ...
- 【自荐】一款简洁、开源的在线白板工具 Drawnix
在线白板工具 Drawnix -- 名字源于绘画(Draw)与凤凰(Phoenix)的灵感交织. Drawnix 的定位是一个开箱即用.开源.免费的在线白板工具产品, 集思维导图.流程图.画笔于一体, ...
- 对接服务升级后仅支持tls1.2,jdk1.7默认使用tls1.0,导致调用失败
背景 如标题所说,我手里维护了一个重要的老项目,使用jdk1.7,里面对接了很多个第三方服务,协议多种多样,其中涉及http/https的,调用方式也是五花八门,比如:commons-httpclie ...
- postman 提示Http Status 400 -Bad Request
Http Status 400 -Bad Request 将headers下面的选项全部勾选 新版postman自带的内容
- 关于我这周的kotlin的学习:
今天学习了kotlin方法的参数和一些lambda的一些知识,其中也是和我们上次日报中讲的方法一样,有三种分类,默认参数,具名参数,可变数量的参数.和以前一样,我们举个例子来理解这个知识点:先是默认方 ...
- LLM应用落地实施手册
背景 自ChatGPT诞生以来,各个企业都开始尝试引入LLM落地实施"智能"应用,而目前并没有太多文章系统地介绍应该怎么落地实施一个基于LLM的应用,到底应该做哪些步骤.本人从20 ...
- 【Python】配置pip使用国内镜像源
配置pip使用国内镜像源 零.问题 使用pip安装插件时总是很慢,咋解决呢? 壹.解决 在桌面上你的文件夹内新建pip目录,一般路径如下:C:\Users\{$你的用户名},比如我的用户名是Minuy ...
- 大模型 Token 究竟是啥:图解大模型Token
前几天,一个朋友问我:"大模型中的 Token 究竟是什么?" 这确实是一个很有代表性的问题.许多人听说过 Token 这个概念,但未必真正理解它的作用和意义.思考之后,我决定写篇 ...