HIT해

[JS 백준] 1654.랜선 자르기 본문

Vue/JavaScript 알고리즘

[JS 백준] 1654.랜선 자르기

힛해 2024. 1. 15. 22:21
728x90

 

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

 

1654번: 랜선 자르기

첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그

www.acmicpc.net

const filePath = process.platform === 'linux' ? '/dev/stdin' : 'input.txt';

let [N,...input] = require("fs")
  .readFileSync(filePath)
  .toString()
  .trim().split('\n');

N = N.split(' ')[1]; // 원하는 랜선 갯수
input = input.map(r=> +r.replace('\r','')).sort((a,b) => a-b);

let max = input[input.length-1];
let min = 1;

while(min<= max){
    let mid = Math.floor((max+min)/2);
    let count = 0;
    for(let i = 0; i < input.length; i++){
        count += Math.floor(input[i]/mid);
    }

    if (count >= N) {
        min = mid + 1;
    } else {
        max = mid - 1;
    }
}

console.log(max);