반응형
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function count(songs, capa) {
let cnt = 1;
let sum = 0;
for (let el of songs) {
if (sum + el > capa) {
cnt++;
sum = el;
} else {
sum += el;
}
}
return cnt;
}
function solution(m, songs) {
let answer;
let lt = Math.max(...songs);
let rt = songs.reduce((a, b) => a + b, 0);
while (lt <= rt) {
let mid = parseInt((lt + rt) / 2);
if (count(songs, mid) <= m) {
answer = mid;
rt = mid - 1;
} else {
lt = mid + 1;
}
}
return answer;
}
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(solution(3, arr));
</script>
</body>
</html>
반응형
'CS (컴퓨터 사이언스) > Algorithm (알고리즘)' 카테고리의 다른 글
[프로그래머스] 해쉬 2. 완주하지 못한 선수 (0) | 2024.06.27 |
---|---|
[알고리즘] 프로그래머스 해시 - 폰켓몬 (0) | 2024.06.03 |
[알고리즘] section7 정렬과 그리디, 결정알고리즘 - 이분탐색 (0) | 2024.05.14 |
[알고리즘] section 6 스택,큐 - 공주구하기 (0) | 2024.05.13 |
[알고리즘] 2. 공통원소구하기 (1) | 2024.01.29 |