-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/LKHcoding/jsAlgorithmStudy/week12/47-1부터N까지숫자중합이10이되는조합구하기.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function solution(N) { | ||
const results = []; | ||
|
||
function backtrack(sum, selectedNums, start) { | ||
if (sum === 10) { | ||
results.push(selectedNums); | ||
return; | ||
} | ||
|
||
for (let i = start; i <= N; i++) { | ||
if (sum + i <= 10) { | ||
backtrack(sum + i, [...selectedNums, i], i + 1); | ||
} | ||
} | ||
} | ||
|
||
backtrack(0, [], 1); | ||
return results; | ||
} | ||
|
||
console.log(solution(5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 백트래킹 | ||
|
||
## 백트래킹이란? | ||
|
||
어떤 가능성이 없는 곳을 알아보고 되돌아 가는 것을 백트래킹 이라 한다. | ||
|
||
## 백트래킹 알고리즘이란? | ||
|
||
가능성이 없는 곳에서는 되돌아가고, | ||
가능성이 있는 곳을 탐색하는 알고리즘을 백트래킹 알고리즘이라 한다. | ||
가능성이 없는 곳은 배제할 수 있으므로 단순 완전 탐색보다는 효율적이다. | ||
|
||
## 유망 함수란? | ||
|
||
해를 찾기위해 탐색해야 하는 경우의 수 중 | ||
해를 찾을 수 없는 케이스는 배제 시키는것 처럼(탐색할 필요가 없음) | ||
특정 조건을 정의 하는 것을 유망 함수를 정의한다고 한다. | ||
|
||
> 1,2,3,4 중 2개를 뽑아 합을 구해서 6 초과하는 해를 찾아야 할때 | ||
> 첫번째 숫자가 1 or 2면 탐색조차 필요없다 |