当前位置:网站首页>Total number of combinations [standard backtracking + backtracking techniques -- reducing stack depth]
Total number of combinations [standard backtracking + backtracking techniques -- reducing stack depth]
2022-06-22 22:22:00 【REN_ Linsen】
DFS Search backtracking
Preface
For permutation and combination problems , It's usually used DFS Search for , State planning changes space for time , Reduce unnecessary double counting . Back to common techniques , Sort the array , Large number / Decimal first , Reduce the function stack depth ; Back to common techniques pruning , Reduce running time .
One 、 Total number of combinations

Two 、 Standard backtracking
1、 Search backtracking
// Combinatorial summation
public class CombinationSum {
/* target: Give some candidate numbers , Then unlimited use , Take and for target The combination of . dfs Just keep looking ( No repeating elements , No zero element ), When and Greater than or equal to target Recursion ends when | Recursion ends when all elements used are used up . */
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// dfs seek
dfs(candidates, 0,0, target, ans, el);
// ans Full
return ans;
}
private void dfs(int[] candidates, int cur,int sum, int target, List<List<Integer>> ans, List<Integer> el) {
if (sum >= target) {
if (sum == target)
ans.add(new ArrayList<>(el));
return;
}
// Standard backtracking
for(int i = cur;i < candidates.length;i++){
el.add(candidates[i]);
dfs(candidates,i,sum + candidates[i],target,ans,el);
el.remove(el.size() - 1);
}
}
}
2、 Reduce stack depth
// Make the recursion depth a little lighter , By reversing the order .
class CombinationSum2 {
/* target: Give some candidate numbers , Then unlimited use , Take and for target The combination of . dfs Just keep looking ( No repeating elements , No zero element ), When and Greater than or equal to target Recursion ends when | Recursion ends when all elements used are used up . */
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// First give candidates Make a sequence , Let big numbers go first , To quickly prune , Reduce the function stack depth .
Arrays.sort(candidates);
// dfs seek
dfs(candidates, candidates.length - 1,0, target, ans, el);
// ans Full
return ans;
}
private void dfs(int[] candidates, int cur,int sum, int target, List<List<Integer>> ans, List<Integer> el) {
if (sum >= target) {
if (sum == target)
ans.add(new ArrayList<>(el));
return;
}
// Standard backtracking
for(int i = cur;i >= 0;i--){
el.add(candidates[i]);
dfs(candidates,i,sum + candidates[i],target,ans,el);
el.remove(el.size() - 1);
}
}
}
summary
1) Standard backtracking ,DFS And dynamic gauge .
2) Sorting reduces stack depth and prunes .
reference
边栏推荐
- Niuke's height in the 52nd monthly race B (thinking problem simulation problem)
- 注意|24日截止 2022年广东二级造价工程师准考证打印入口开通
- 校园跑腿管理端APP—陕西格创
- Why do you think it is common for Chinese people to earn more than 10000 yuan a month?
- 腾讯云上传文件出现的问题:in a frame because it set ‘X-Frame-Options‘ to ‘deny‘.
- Wechat applet batch submission for review
- CyCa children's physique etiquette Shenzhen training results assessment successfully concluded
- IDC publie le rapport sur la gouvernance des données en Chine
- Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
- [path planning] week 1: hodgepodge
猜你喜欢

【持续更新中...】2021年全国大学生电子设计大赛 (三)匿名四轴拓空者飞控系统设计解读

考生必读篇 | PMP6月25日考试临近,需要注意什么?

redis 报错解决与常用配置

For an unforgettable memory: special topic of Sun Jian

校园跑腿管理端APP—陕西格创

CyCa children's physique etiquette Shenzhen training results assessment successfully concluded

pycharm 配置远程连接服务器开发环境

Implementation of depth traversal adjacency table in Figure 6-7

【路径规划】第一周: 大杂烩

【知乎知识主推荐】 无人机中的城堡- 专注于无人机在不同技术领域的应用
随机推荐
shell(34) : 时间
Cannot re register id: pommeffacompetition-v0 problem solving
There are 15 necessary knowledge points for the second level cost engineer before the exam! I wish you success!
6-3 non recursive traversal of binary tree
[GWCTF 2019]mypassword XSS
6-1 operation set of binary search tree
Linux安装Mysql(包成功!!)
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
A case of 94 SQL optimization (the writing method used is often rejected)
Live broadcast forecast the sixth issue of the webinfo lecture hall of the China Information Association will be broadcast on June 24
Lesson 032: exception handling: you can't always be right | after class test questions and answers
【知乎知识主推荐】 无人机中的城堡- 专注于无人机在不同技术领域的应用
Analysis of fegin
A hundred lines of code to realize reliable delay queue based on redis
【MAVROS】MAVROS 啓動指南
RapidEye快鸟、SPOT卫星遥感影像数据
Icml2022 | using virtual nodes to promote graph structure learning
Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool
考生必读篇 | PMP6月25日考试临近,需要注意什么?
322. change exchange