<script>
const permutation = (arr, n) => {
let result = [];
if (n === 1) return arr.map((v) => [v]);
arr.forEach((v, idx, arr) => {
const fix = v;
const rest = arr.filter((_, index) => index !== idx);
const permuationArr = permutation(rest, n - 1);
const combineFix = permuationArr.map((v) => [fix, ...v]);
result.push(...combineFix);
});
return result;
}
</script>
<script>
const getCombinations = (arr, n) => {
const results = [];
if (n === 1) return arr.map((v) => [v]);
arr.forEach((fix, i, origin) => {
const rest = origin.slice(i + 1);
const combinations = getCombinations(rest, n-1);
const attached = combinations.map((v) => [fix, ...v])
results.push(...attached);
});
return results;
}
</script>
<script>
let arr = new Array(n).fill().map((_, i) => new Array(i+1));
// Array(n).fill() : 길이가 n인 빈 배열 만듦
// 그 상태에서 map으로 i+1길이의 배열을 만드므로 길이가 1 ~ n인 배열이 있는 2차원 배열이 됨.
</script>
<scrip>
let arr = [[1], [1,2,3], [5,6]]
arr.flat() // [1,1,2,3,5,6]
</script>
남들은 dfs, bfs로 푸는 문제를 순열, 조합으로 풀고있는 나...
빨리 dfs, bfs 공부해야겠다 😥