[JS/백준]{완전탐색/브루트포스}(2798) 블랙잭

202210월 05

백준 문제 링크

1

문제 설명

완전탐색 문제입니다. 모든 조합을 구해서 가장 m과 근접한 값을 찾으면 됩니다.


코드

const getCombinations = function (arr, selectNumber) {
  const results = [];
  if (selectNumber === 1) return arr.map((el) => [el]);

  arr.forEach((fixed, index, origin) => {
    const rest = origin.slice(index + 1);
    const combinations = getCombinations(rest, selectNumber - 1);
    const attached = combinations.map((el) => [fixed, ...el]);
    results.push(...attached);
  });
  return results;
};

let input = require("fs")
  .readFileSync(process.platform === "linux" ? "dev/stdin" : "input.txt")
  .toString()
  .trim()
  .split("\n");

const [n, m] = input[0].split(" ").map(Number);
const cardList = input[1].split(" ").map(Number);

const combi = getCombinations(cardList, 3);

let maxVal = -1;

combi.forEach((val, idx) => {
  const result = val.reduce((pre, cur) => pre + cur);
  if (maxVal < result && result <= m) {
    maxVal = result;
  }
});
console.log(maxVal);