PS/백준

백준 / 이분 탐색 / 16401번 / 과자 나눠주기 / JS

KimMinJun 2022. 11. 17. 03:31

< 문제 바로가기 >

 

16401번: 과자 나눠주기

첫째 줄에 조카의 수 M (1 ≤ M ≤ 1,000,000), 과자의 수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에 과자 N개의 길이 L1, L2, ..., LN이 공백으로 구분되어 주어진다. 과자의 길이는 (1 ≤ L1, L2, ..., LN ≤ 1,

www.acmicpc.net

 

/* 과자 나눠주기 */
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : '../input.txt';
// const filePath = process.platform === 'linux' ? '/dev/stdin' : 'BOJ/input.txt';
const input = fs.readFileSync(filePath).toString().trim().split('\n');

const [M, N] = input.shift().split(' ').map(Number);
const cookieLengthList = input.shift().split(' ').map(Number);

function solution(M, N, cookieLengthList) {
  let min = 1;
  let max = Math.max(...cookieLengthList);

  cookieLengthList = cookieLengthList.sort((a, b) => a - b);

  while (min <= max) {
    let mid = Math.floor((min + max) / 2);

    // 각 길이를 mid로 나눴을때의 수를 모두 더해준다
    let cnt = cookieLengthList.reduce(
      (acc, cur) => acc + Math.floor(cur / mid)
    , 0);

    // 모두 더한 수가 M보다 크다면 더 큰수로 나눠야 값이 작아지므로
    // min을 더 크게 한다.
    if(cnt >= M) min = mid + 1;
    // 모두 더한 수가 M보다 작다면 더 작은수로 나눠야 값이 커지므로
    // max를 더 작게 한다.
    else max = mid - 1;
  }

  console.log(max);
}

solution(M, N, cookieLengthList);