(백준) 국회의원 선거 성공

hwisaac·2024년 11월 20일
0

코테TIL

목록 보기
15/20

문제링크

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

풀이

const filePath = process.platform === 'linux' ? 0 : './input.txt';
let [N, me, ...arr] = require('fs')
  .readFileSync(filePath)
  .toString()
  .trim()
  .split('\n')
  .map(Number);

function solve() {
  if (N === 1) {
    console.log(0);
    return;
  }
  const init = me;
  while (Math.max(...arr) >= me) {
    const maxIndex = arr.indexOf(Math.max(...arr));
    arr[maxIndex] -= 1;
    me += 1;
  }

  return console.log(me - init);
}

solve();

0개의 댓글