原题链接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进阶路的更多相关文章

  1. [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 ...

  2. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  3. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  4. [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 ...

  5. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. Shell - [11] 开源Apache Zookeeper集群启停脚本

    一.集群角色部署 当前有Zookeeper集群如下 主机名 ctos79-01 ctos79-02 ctos79-03 Zookeeper ○ ○ ○ 二.脚本使用 三.脚本内容 #!/bin/bas ...

  2. 面试题53 - I. 在排序数组中查找数字 I

    地址:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/ <?php /** 面试题53 ...

  3. 提示词工程——AI应用必不可少的技术

    引言 在人工智能技术飞速发展的今天,大语言模型(LLM)已成为推动技术革新的核心引擎.然而,如何让这些"聪明"的模型真正落地业务场景.解决实际问题?答案往往不在于模型本身的参数规模 ...

  4. Python字符串前缀u、r、b、f含义(转)

    1.字符串前加 u 例子: u"字符串中有中文" 含义: 前缀u表示该字符串是unicode编码,Python2中用,用在含有中文字符的字符串前,防止因为编码问题,导致中文出现乱码 ...

  5. Bringing machine 'default' up with 'virtualbox' provider... Your VM has become "inaccessible." Unfortunately, this is a critical error with VirtualBox that Vagrant can not cleanly recover from.

    启动虚拟机报错 vagrant up Bringing machine 'default' up with 'virtualbox' provider...Your VM has become &qu ...

  6. Destination host unreachable 一般解决办法

    症状: 上网各类应用基本正常,但是在命令行下使用ping命令,无论任何地址,均反馈Destination host unreachable. 分析: 输入命令arp -a可以看到网关的MAC地址正常解 ...

  7. Delphi 让窗体自适应屏幕显示

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  8. [python] 使用Python实现Markdown文档格式转换

    本文主要介绍如何利用Python中的MarkItDown库将多种文件高效转换为Markdown文本,以及如何使用Python-Markdown库将Markdown文本转换为HTML(超文本标记语言)文 ...

  9. kubernetes mysql-StatefulSet报错处理

    我们使用网上mysql-StatefulSet集群教程时候mysql-1启动错误,init-error. 第一次尝试解决:我从官网上下载yaml部署依然报错. 第二次尝试解决:网上换各种版本的yaml ...

  10. 【Docker】命令行操作

    Docker常用命令 帮助命令 docker version docker info docker --help Docker 客户端 docker 客户端非常简单 ,我们可以直接输入 docker ...