반응형
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 |
Tags
- 자바스크립트
- Nestjs
- socket
- 레벨 1
- 프로그래머스
- 수학
- typescript
- TCP
- 타입 챌린지
- ip
- Node.js
- 쉬운 문제
- 크롤링
- dp
- 문자열
- BFS
- 타입스크립트
- 그래프
- 백준
- 가천대
- 알고리즘
- HTTP
- javascript
- Algorithm
- dfs
- 프로그래머스 레벨 2
- HTTP 완벽 가이드
- Crawling
- type challenge
- 소켓
Archives
- Today
- Total
kakasoo
[node.js] 모든 순열 ( 백준 10974번 ) 본문
반응형
// 백준 10974번 모든 순열을 풀었습니다.
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", (line) => {
const elements = new Array(Number(line)).fill(0).map((el, i) => i + 1);
const result = [...permutations(elements)].map((el) => el.join(" ")).sort();
for (const a of result) {
console.log(a);
}
});
/**
*
* @param {number[]} elements
*/
function* permutations(elements) {
if (elements.length === 1) {
yield elements;
} else {
const [first, ...rest] = elements;
for (const a of permutations(rest)) {
for (let i = 0; i < elements.length; i++) {
const start = a.slice(0, i);
const ended = a.slice(i);
yield [...start, first, ...ended];
}
}
}
}
혹시라도 이 내용을 보는 분이 있다면, 순열에 대한 공식은 그냥 외우라고 하고 싶다.
C++이나 파이썬에서는 순열과 조합이 제공되는 것 같던데, JS에는 그런 게 없다.
제너레이터가 생소하다면 한 번 외워둘 만 하다.
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] 단어 수학 ( 백준 1339번) (0) | 2021.07.31 |
---|---|
[node.js] 차이를 최대로 ( 백준 10819번 ) (0) | 2021.07.30 |
[node.js] 수 이어 쓰기 1 ( 백준 1748번 ) (0) | 2021.07.29 |
[node.js] 리모컨 ( 백준 1107번 ) (0) | 2021.07.29 |
[node.js] n진수 게임 ( 프로그래머스 레벨2 ) (0) | 2021.07.07 |