Skip to content

Commit 33c79ee

Browse files
Merge pull request #653 from eric-hjh/main
[황장현] 97차 라이브 코테 제출
2 parents 8a93371 + a5e7a85 commit 33c79ee

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

live9/test97/문제3/황장현.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function solution(distance, rocks, n) {
2+
rocks.sort((a, b) => a - b);
3+
const temp = [0, ...rocks, distance];
4+
let left = 0;
5+
let right = distance;
6+
let answer = 0;
7+
8+
while (left <= right) {
9+
const mid = Math.floor((left + right) / 2);
10+
let prev = 0;
11+
let count = 0;
12+
13+
for (const rock of temp) {
14+
if (rock - prev < mid) {
15+
count++;
16+
if (n < count) break;
17+
} else {
18+
prev = rock;
19+
}
20+
}
21+
22+
if (n < count) {
23+
right = mid - 1;
24+
} else {
25+
answer = mid;
26+
left = mid + 1;
27+
}
28+
}
29+
30+
return answer;
31+
}
32+
33+
console.log(solution(25, [2, 14, 11, 21, 17], 2));

0 commit comments

Comments
 (0)