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 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,否则后面没法做。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- dfs的target直接写成target就行了,就是个参数
[二刷]:
- 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组的更多相关文章
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 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 ...
- [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指定了 ...
- [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 ...
- [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 Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- 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 ...
随机推荐
- 用virtualenv建立独立虚拟环境 批量导入模块信息
pip3 install virtualenv mkdir env/env1 source bin/activate pip3 freeze >requirements.txt or pipre ...
- Session、Cookie、Cache、Token分别是什么及区别
一.Session 1 )Session 解释 Session 是单用户的会话状态.当用户访问网站时,产生一个 sessionid.并存在于 cookies中.每次向服务器请求时,发送这个 cooki ...
- 初探JavaScript的截屏实现
最近参与了网易炉石盒子的相关页面开发,在做卡组分享页(地址:炉石盒子卡组分享),有个需求:用户可以把这个卡组以图片的形式分享给好友.最初的的做法是使用服务器把该页面转换成图片,然后把图片地址返回给前端 ...
- Java day1
1. 学习java,首先是jdk的安装,JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+ ...
- django 获取用户提交的数据 文件 表单
templates: <div> <form action="/detail" method="post" enctype="mul ...
- 【python】Numpy中stack(),hstack(),vstack()函数详解
转自 https://blog.csdn.net/csdn15698845876/article/details/73380803 这三个函数有些相似性,都是堆叠数组,里面最难理解的应该就是stack ...
- python修改字典的值(update map value)
mydict.update({'newkey':'newvalue'})
- USB3.0及NVME SSD安装WIN7X64
USB3.0及NVME SSD安装WIN7X64https://tieba.baidu.com/p/4822034273?pn=1所有的人都是菜鸟过来的,不过有些人懂得自己动手找到答案:有些人则是懒得 ...
- [UE4]Spin Box,数字输入,可拖动
一.Spin Box在Input组下 二.Spin Box的文字样式可以在Spin Box.Display中修改 三.Spin Box事件 1.On Value Changed:值改变时触发 2.On ...
- 【阿里云服务器】外网无法访问tomcat下部署的项目
问题提出:在ESC实例上部署了jdk和tomcat(略,上云了,上云了),启动tomct后,内网可以访问8080端口,外网无法访问8080. 系统环境:winsdow 2008 企业版 解决方案: 在 ...