250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 구름톤챌린지
- Leetcode #javascript #알고리즘 #Algorithms #js
- nextjs
- 최솟갑 구하기
- Hermes Engine
- 헤르메스 엔진
- 구름톤
- ResizeObserver
- 테스트 Date
- Jest uuid syntax
- 구름톤 챌린지
- 연결 요소 제거하기
- nextjs-performance
- 프로그래머스
- 호텔 대실
- JavaScript
- 리액트네이티브
- 귤 고르기
- 테이블 해시 함수
- 통신망분석
- mock date
- create-next-app
- Google 애널리틱스
- 날짜 테스트
- 자바스크립트
- jest
- 중첩 점
- mutationobserver
- 과제 진행하기
- 리액트네이티브 엔진
Archives
- Today
- Total
나만보는개발공부블로그
Permutation과 Combinations 본문
Permutations (순열)
서로 다른 n 개 중 r 개를 골라 순서를 고려해 나열한 경우의 수
nPr = n·(n-1)·(n-2)···(n-r+1)
function allPermutations (items) {
// allPermutations () : return a list of all possible permutations
// credits: https://stackoverflow.com/questions/9960908/permutations-in-javascript
let results = [];
function permute (arr, memo) {
var cur, memo = memo || [];
for (let i = 0; i < arr.length; i++) {
cur = arr.splice(i, 1);
if (arr.length === 0) {
results.push(memo.concat(cur));
}
permute(arr.slice(), memo.concat(cur));
arr.splice(i, 0, cur[0]);
}
return results;
}
permute(items);
return results;
}
var fruits = ["Apple", "Banana", "Coconut"];
var permutated = allPermutations(fruits);
console.table(permutated);
Combinations (조합)
서로 다른 n개 중에서 r개(n≥r) 취하여 조를 만들 때, 이 하나하나의 조를 n개 중에서 r개 취한다.
nCr = n(n−1)(n−2)⋯⋯(n−r+1) / r!
function allCombinations (items) {
// allcombinations () : return a list of all possible combinations
let results = [];
for (let slots = items.length; slots > 0; slots--) {
for (let loop = 0; loop < items.length - slots + 1; loop++) {
let key = results.length;
results[key] = [];
for (let i = loop; i < loop + slots; i++) {
results[key].push(items[i]);
}
}
}
return results;
}
var fruits = ["Apple", "Banana", "Coconut"];
var combo = allCombinations(fruits);
console.table(combo);
* 알고리즘 문제를 풀때 순열 및 조합 문제들이 나왔을때 참고하면 좋습니다.
'Algorithms' 카테고리의 다른 글
[프로그래머스] 최솟값 만들기 (0) | 2023.09.15 |
---|---|
우선순위 큐 (0) | 2023.09.14 |
[PCCP모의고사] 체육대회 - Javascript (0) | 2023.09.11 |
[PCCP모의고사] 외톨이 알파벳 - Javascript (0) | 2023.09.10 |
[구름] 소금물의 농도 구하기 (0) | 2023.09.09 |