当前位置:网站首页>Backtracking to solve combinatorial problems
Backtracking to solve combinatorial problems
2022-07-25 02:54:00 【One is dozing】
1. Combine (leetcode77)
Given n,k, from 1-n Choose from k Number
prune : When the number of subsequent elements is insufficient , To reduce branches ,<=n-(k-path.length)+1
var combine = function(n, k) {
let res = [],path=[];
function combineHelper(n,k,startIndex){
if(path.length == k){
res.push([...path]);
return;
}
for(let i = startIndex;i<=n-(k-path.length)+1;i++){
path.push(i);
combineHelper(n,k,i+1);
path.pop();
}
}
combineHelper(n,k,1);
return res;
};2. Total number of combinations III(leetcode216)
Find and for n Of k Number , Only 1-9
prune : When the number of subsequent elements is insufficient , To reduce branches ,<=9-(k-path.length)+1
var combinationSum3 = function(k, n) {
let path = [],res = [];
function backtracking(start,sum){
if(path.length == k){
if(sum == n){
res.push([...path]);
}
return;
}
// Pruning
for(let i = start;i<=9-(k-path.length)+1;i++){
// for(let i = start;i<=9;i++){
path.push(i);
backtracking(i+1,sum+i);
path.pop();
}
}
backtracking(1,0);
return res;
};3. Combinatorial summation (leetcode39)
From a non repeating array nums in , Find and target Array of
Be careful : Array nums The elements in can be retrieved repeatedly
Pruning : Prioritize , When nums[i]>target when , Pruning
var combinationSum = function(candidates, target) {
let res = [],path = [];
candidates.sort();
function backtracking(j,sum){
if(sum > target) return;
if(sum == target){
res.push([...path]);
return;
}
for(let i = j;i<candidates.length;i++){
const n = candidates[i];
if(n>target-sum) continue;
path.push(n);
sum += n;
// candidates The array in can be reused
backtracking(i,sum);
path.pop();
sum -= n;
}
}
backtracking(0,0);
return res;
};4. Combinatorial summation III(leetcode40)
Duplicate array from nums in , Find and target Array of , Every nums[i] It can only be used once .Nums Repeat , but res Cannot have duplicate arrays in .
Be careful : Array nums Elements in cannot be retrieved repeatedly
Pruning : Prioritize , When nums[i]>target when , Pruning
duplicate removal : Use used, De duplication of tree layer
var combinationSum2 = function(candidates, target) {
let res = [],path = [];
let len = candidates.length;
candidates.sort((a,b)=>a-b);
let used = new Array(len).fill(false);
function backtrcking(startIndex,sum){
if(sum > target) return;
if(sum == target){
res.push([...path]);
return;
}
for(let i = startIndex;i<len;i++){
let cur = candidates[i];
if(cur > target-sum || (i>0 && candidates[i] == candidates[i-1] && !used[i-1])) continue;
if (used[i] == true) continue;
path.push(cur);
sum += cur;
used[i] = true;
backtrcking(i+1,sum);
path.pop();
sum -= cur;
used[i] = false;
}
}
backtrcking(0,0);
return res;
};边栏推荐
- JS foundation -- hijacking of this keyword
- Physical experiment simulation
- R language one page and many pictures
- How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
- DOM node type
- TS uses a third-party library, and there is no type declaration file error handling
- JS written test -- regular expression
- [C language] program compilation (preprocessing)
- StrError and PERROR
- Matlab for circular pit
猜你喜欢
![[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed](/img/e7/b9ecf49721e6304a2d8ca824b64458.png)
[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed
![[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality](/img/12/1763571a99e6ef15fb7f9512c54e9b.png)
[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality

Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)

Keil compile download error: no algorithm found for: 08000000h - 08001233h solution

Jenkins plug-in development -- plug-in expansion

Explanation of C language file operation function
![[C language] program compilation (preprocessing)](/img/94/175a84d89b1f16987e529eb029cbbc.png)
[C language] program compilation (preprocessing)

6.0 cancellation of member registration verification code

JS foundation -- task queue and event loop

Learning record XIII
随机推荐
Explorer TSSD 2019 software installation package download and installation tutorial
Object.defineproperty use
Routing policy interferes with routing
Conceptual distinction between Po, Bo, VO, dto and POJO
Domestic edge computing organization and product research
"Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
JS foundation -- object static method
On the economic model of open source ecology
6.0 cancellation of member registration verification code
Custom types in C language
Simulation Implementation of string function (Part 1)
Sequence diagram of UML diagram series
HAC cluster is modified to stand-alone
Class notes (4) (2) -- 572. Compete
How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
JS written test -- regular expression
Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
Operator explanation - C language
[jailhouse article] scheduling policies and system software architectures for mixed criticality