[抄题]:

Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into knon-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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

dfs中的cc是:组数k直接等于1

dfs的退出条件是:cur_sum == target

[思维问题]:

不懂“是否”题为啥要用dfs:这一步能不能、下一步能不能,每个元素都要算到,所以用boolean DFS

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

DFS扩展的依据是:下一步没有访问过 visited = f,就设置visited = t &回溯。中间不要直接return,否则后面没法做。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. dfs的target直接写成target就行了,就是个参数

[二刷]:

  1. dfs的start直接写成start就行了,就是个参数 traverse中要变成i + 1

[三刷]:

找到k组之后,继续找k - 1 组,变量变化之后就要控制它的最后值 是否为0或1 了

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

有变量k的次数递减,就必须写退出条件。DFS最重要的反而是退出条件

[复杂度]:Time complexity: O(n) Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum = 0; //ini: get sum
for (int num : nums) sum += num;
int[] visited = new int[nums.length]; //cc: k == 1
if (k == 1) return true;
if (k <= 0 || sum % k != 0) return false; //canPartition
return canPartition(0, nums, visited, 0, k, sum / k);
} public boolean canPartition(int start, int[] nums, int[] visited, int curSum, int k, int target) {
if(k==1) return true; //exit :curSum == target
if (curSum == target) return canPartition(0, nums, visited, 0, k - 1, target); //backtracing
for (int i = start; i < nums.length; i++) {
if (visited[i] == 0) {
visited[i] = 1;
//do not return directly
if(canPartition(i + 1, nums, visited, curSum + nums[i], k, target)) return true;
visited[i] = 0;
}
} return false;
}
}

698. Partition to K Equal Sum Subsets 数组分成和相同的k组的更多相关文章

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

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

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

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

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

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

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

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

  7. LeetCode Partition to K Equal Sum Subsets

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

  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. [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. 关于动态内存malloc和realloc

    1.malloc   1.申请的内存长度可以运行时决定,单位是字节  2.申请的内存为连续的内存空间  3.返回的地址可以根据实际需要强转成对应的类型  4.动态申请内存的生命周期是整个程序,除非手动 ...

  2. 互联网同步yum服务器,中科大 rsync createrepo

    参考文章 https://blog.csdn.net/chenjia6605/article/details/82734945 1.本机安装所需工具: yum -y install rsync cre ...

  3. Elasticsearch -- Head插件安装

    安装Head插件 由于head插件本质上还是一个nodejs的工程,因此需要安装node,使用npm来安装依赖的包. <1>安装Node.js 下载解压 wget https://node ...

  4. 一个可以再WIN2000及以上操作系统使用的GetTickCount64

    以后尽量用Delphi/C++双代码写. GetTickCount返回值是整数,这样的话最多49天多就会复位重新从0开始.Vista以后提供了GetTickCount64这个函数,但是WindowsX ...

  5. MIDAS.dll 出错时 (Error loading MIDAS.DLL.)

    DELPHI 写的程序会出 ---------------------------Pmain---------------------------Error loading MIDAS.DLL.--- ...

  6. android升级gradle到3.4.1

    这两天把gradle升级到了gradle-3.4.1 com.android.tools.build:gradle升级到了com.android.tools.build:gradle:2.3.0 结果 ...

  7. nginx gzip配置

    参考: https://docs.nginx.com/nginx/admin-guide/web-server/compression/ server { gzip on;    gzip_types ...

  8. Container 、Injection

    Container: Linux容器作为一类操作系统层面的虚拟化技术成果,旨在立足于单一Linux主机交付多套隔离性Linux环境.与虚拟机不同,容器系统并不需要运行特定的访客操作系统.相反,容器共享 ...

  9. HDFS知识点总结

    学习完Hadoop权威指南有一段时间了,现在再回顾和总结一下HDFS的知识点. 1.HDFS的设计 HDFS是什么:HDFS即Hadoop分布式文件系统(Hadoop Distributed File ...

  10. requests_html 使用

    安装 pip install requests-html #2种方式爬取 博客园from requests_html import HTMLSession session=HTMLSession() ...