728x90

😢 중복 조합 문제
😊 각 숫자를 중복으로 뽑아도 되지만,
조합된 숫자가 중복되면 안되므로 전에 뽑았던 숫자를 제외 해야한다.
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
// N and M (4) Combination of repetition | |
// For submit | |
// const fs = require('fs'); | |
// const input = fs.readFileSync('/dev/stdin').toString().trim().split(' ').map(nm => parseInt(nm)); | |
// For local test | |
const input = [3, 3]; | |
const N = input.shift(); | |
const M = input.shift(); | |
const visited = new Array(N); | |
const output = []; | |
let result = ''; | |
function dfs(idx, cnt) { | |
if (cnt === M) { | |
result += `${output.join(' ')}\n`; | |
return; | |
} | |
for (let i = idx; i < N; i++) { | |
output.push(i + 1); | |
dfs(i, output.length); | |
output.pop(); | |
} | |
} | |
dfs(0, 0); | |
console.log(result); |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
The Modern JS Tutorial: array-methods: filterRange (0) | 2021.09.13 |
---|---|
The Modern JS Tutorial: array-methods: camelize (0) | 2021.09.13 |
백준 15651번: N과 M (3) Node.js(JavaScript) (0) | 2020.02.25 |
백준 15650번: N과 M (2) Node.js(JavaScript) (0) | 2020.02.24 |
백준 15649번: N과 M (1) Node.js(JavaScript) (0) | 2020.02.24 |