[백준] 2864 5와 6의 차이 - Node.js

송철진·2023년 4월 6일
0

백준-Node.js

목록 보기
57/69

문제

https://www.acmicpc.net/problem/2864

Solution

const input = require('fs').readFileSync('/dev/stdin').toString().trim()

const solution = (input) => {
  const min = input.replaceAll(/[6]/g, 5).split(' ').map(Number).reduce((a,b)=>a+b,0)
  const max = input.replaceAll(/[5]/g, 6).split(' ').map(Number).reduce((a,b)=>a+b,0)
  return min +' '+ max
}

console.log(solution(input))

최소값은 6을 모두 5로 바꿨을 때의 합이고
최대값은 5를 모두 6으로 바꿨을 때의 합이다.

replaceAll과 정규식을 사용해서 값을 바꿨고
reduce를 사용해서 배열의 합을 구했다.

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글