백준 1417 javascript

인지용·2024년 11월 2일
0

알고리즘

목록 보기
3/46

https://www.acmicpc.net/status?user_id=qwef123&problem_id=1417&from_mine=1


const input = require('fs')
    .readFileSync(process.platform === 'linux' ? '/dev/stdin' : './data.txt')
    .toString()
    .trim()
    .split('\n')
    .map(Number);

const N = input[0];

//후보가 한명인 경우
if(N == 1){
    console.log(0);
    return;
}

//후보1번을 제외한 후보들 배열 생성
let arr = new Array(N-1);
for(let i=0; i<N-1; i++) {
    arr[i] = parseInt(input[i+2])
}

let count = 0;
let me = input[1];

call(arr);

console.log(count)

//재귀 함수를 이용해서
function call(list) {
    list.sort(function (a, b) {
        return b - a;
    });

    if(me > list[0]){
        return;
    }

    count++;
    me++;
    
    list[0] = list[0] - 1;    
    return call(list)
}

제일 표가 많은 후보의 표를 1씩 감소해야 최소 매수횟수가 나온다.

그래서 기호1번을 제외한 나머지 후보들의 표를 내림차순 정렬 후
표를 1씩 감소하도록 재귀함수를 이용해서 해결했다.

profile
한-줄

0개의 댓글