All'alba vincerò

At dawn, I will win!

Algorithm

[백준] 15663번: N과 M (9)

나디아 Nadia 2024. 10. 15. 09:35

🛠️ 문제 

15663번: N과 M (9)

 

 

💡 풀이

const fs = require('fs');
const path = require('path');

const input = fs.readFileSync(path.join(__dirname, 'input.txt')).toString().trim().split('\n');


const [N, M] = input[0].split(' ').map(Number);
const numbers = Array.from(new Set(input[1].split(' ').map(Number))).sort((a, b) => a - b);

const result = [];
const visited = new Array(numbers.length).fill(false);

function backtrack(combination) {
  if (combination.length === M) {
    result.push(combination.join(' '));
    return;
  }

  for (let i = 0; i < numbers.length; i++) {
    if (visited[i]) continue;

    if (i > 0 && numbers[i] === numbers[i - 1] && !visited[i - 1]) continue;

    visited[i] = true;
    backtrack([...combination, numbers[i]]);
    visited[i] = false; 
  }
}

backtrack([]);

console.log(result.join('\n'));

 

 

 

Algorithm-study/Algorithm/Solving/Backtracking/15663.js at main · kwonboryong/Algorithm-study

알고리즘(algorithm) 문제 풀이 스터디 . Contribute to kwonboryong/Algorithm-study development by creating an account on GitHub.

github.com

 

3주차 - 2일 | Notion

💡문제 분석 요약

thin-brisket-ae4.notion.site