-
Notifications
You must be signed in to change notification settings - Fork 0
/
입국심사.js
49 lines (38 loc) · 1.04 KB
/
입국심사.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Solution 1
function solution1(n, times) {
const _times = times.slice();
_times.sort((a, b) => a - b);
let leftPoint = 1;
let rightPoint = n * _times[_times.length - 1];
let minTotalTime = rightPoint;
while (leftPoint <= rightPoint) {
const mid = Math.floor((leftPoint + rightPoint) / 2);
let count = 0;
times.forEach((time) => {
count += Math.floor(mid / time);
if (count >= n) {
minTotalTime = Math.min(minTotalTime, mid);
return;
}
});
if (count >= n) {
rightPoint = mid - 1;
} else {
leftPoint = mid + 1;
}
}
return minTotalTime;
}
// Solution 2
function solution2(n, times) {
const _times = times.slice();
const sortedTimes = _times.sort((a, b) => a - b);
let left = 1;
let right = sortedTimes[sortedTimes.length - 1] * n;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const sum = times.reduce((acc, time) => acc + Math.floor(mid / time), 0);
sum < n ? (left = mid + 1) : (right = mid - 1);
}
return left;
}