문제
[boj] 1475. 방 번호 (node.js)
풀이
핵심 풀이
const ceil = Math.ceil((numbers[6] + numbers[9]) / 2);
return Math.max(...Object.values({ ...numbers, 6: ceil, 9: ceil }));
- 최근에 공부한 게 재미있는 리액트이다보니 Object destructing 을 즐겨 사용하고 있다. 로직에서 핵심은 "6과 9는 동일한 역할을 한다"는 점인데, 따라서 6과 9는 그 총 개수를 합산한 후 2로 나누고, 올림처리 해주면 최대로 필요한 개수를 구할 수 있다. 나머지 숫자들은 Math.max를 적용하면 된다.
전체 풀이
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const solution = () => {
const N = input();
let numbers = { ...new Array(10).fill(0) };
N.split("").forEach((num) => numbers[num]++);
const ceil = Math.ceil((numbers[6] + numbers[9]) / 2);
return Math.max(...Object.values({ ...numbers, 6: ceil, 9: ceil }));
};
let _line = 0;
const input = () => stdin[_line++];
let stdin = [];
rl.on("line", function (line) {
stdin.push(line);
}).on("close", function () {
console.log(solution());
process.exit();
});