Skip to content

Commit 06b68bb

Browse files
author
임빛나
committed
알고리즘 풀이 업데이트
1 parent 9b1f3a9 commit 06b68bb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

section7/11.뮤직비디오.html

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function count(songs, capa) {
9+
let cnt = 1;
10+
let sum = 0;
11+
12+
for (let el of songs) {
13+
if (sum + el > capa) {
14+
cnt++;
15+
sum = el;
16+
} else {
17+
sum += el;
18+
}
19+
}
20+
return cnt;
21+
}
22+
23+
function solution(m, songs) {
24+
let answer;
25+
let lt = Math.max(...songs);
26+
let rt = songs.reduce((a, b) => a + b, 0);
27+
while (lt <= rt) {
28+
let mid = parseInt((lt + rt) / 2);
29+
if (count(songs, mid) <= m) {
30+
answer = mid;
31+
rt = mid - 1;
32+
} else {
33+
lt = mid + 1;
34+
}
35+
}
36+
return answer;
37+
}
38+
39+
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
40+
console.log(solution(3, arr));
41+
</script>
42+
</body>
43+
</html>

0 commit comments

Comments
 (0)