[抄题]:

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. Linux设备驱动模型之platform(平台)总线详解

    /********************************************************/ 内核版本:2.6.35.7 运行平台:三星s5pv210 /*********** ...

  2. Go语言开发Windows应用

    Go语言开发Windows应用 当第一次看到Go程序在windows平台生成可执行的exe文件,就宣告了windows应用也一定是Go语言的战场.Go不是脚本语言,但却有着脚本语言的轻便简单的特性.相 ...

  3. centos7使用snmp

     一.安装snmp net-snmp :服务端 net-snmp-utils:客户端工具集   二.启动 systemctl start snmpd   三.修改配置文件(完整的配置文件如下) com ...

  4. 中文分词算法工具hanlp源码解析

    词图 词图指的是句子中所有词可能构成的图.如果一个词A的下一个词可能是B的话,那么A和B之间具有一条路径E(A,B).一个词可能有多个后续,同时也可能有多个前驱,它们构成的图我称作词图. 需要稀疏2维 ...

  5. zookeeper的WEB客户端zkui使用

    转载自:http://blog.csdn.net/csolo/article/details/53694665 前面几篇实践说明了zookeeper如何配置和部署,如何开发,因为大多是后台操作,对于维 ...

  6. VUE 高德地图选取地址组件开发

    高德地图文档地址 http://lbs.amap.com/api/lightmap/guide/picker/ 结合步骤: 1.通过iframe内嵌引入高德地图组件 key就选你自己申请的key &l ...

  7. DataTable.Select 处理关联表数据

    DataSet.Clone  会拷贝表结构,关联关系也会拷贝, 用Select 筛选后ImportRow 导入新的DataTable,然后处理关联DataTable DataSet ds2 = dsS ...

  8. 移植Valgrind检测Android JNI内存泄漏

    1.相关工具 Valgrind:从Valgrind官网下载最新的源码包,我这里用的是:valgrind 3.14.0 (tar.bz2) [17MB] - 9 October 2018. Ubuntu ...

  9. 【OpenStack】network相关知识学习

    network 类型 local:通信不跨主机,必须同一网段,主要做单机测试使用: flat:统计可以跨主机,但是需要在同一网段: 每个 flat network 都会独占一个物理网卡 计算节点上 b ...

  10. Centos7.5 安装高版本Cmake 3.6.2

    下载Cmake wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz 解压Cmake tar xvf cmake-3.6.2.tar.gz & ...