Skip to content

Commit dad97f2

Browse files
committed
[Week14] PGS 43238: 입국심사
1 parent bef6203 commit dad97f2

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package study.week14;
2+
3+
// 입국 심사
4+
// 이분 탐색
5+
6+
/**
7+
* 이분 탐색이라는 걸 알고 풀었다.
8+
* times 에서 최대값을 찾고 최대의 시간을 소요하는 값(최대시간 * 인원수) 를 right로 둔다.
9+
* 이분탐색을 돌리는데 비교 값은 선택된 mid 기반으로 각 time 들이 mid 시간 내 몇명을 처리할 수 있는지를 합산하여
10+
* 해당 결과가 실제 처리해야하는 인원수보다 작으면 left값을 올리고, 크다면 answer를 업데이트하고 right값을 mid 로 한다.
11+
* 크거나 같으면 업데이트하고, mid-1이 아닌 mid로 하는 이유는,
12+
* 적합한 시간 즉, 최소값을 찾는 이분 탐색에서 우리가 찾는 건 check(mid) >= n 을 만족하는(n보다 작으면 불가능) 가장 작은 값이기 때문이다.
13+
* 또한 mid-1을 하게 되면 유일한 정답을 잃어버릴 수도 있기 때문에 정답 후보군을 유지하는 것
14+
*/
15+
public class PGS_43238 {
16+
class Solution {
17+
public long solution(int n, int[] times) {
18+
long answer = 0;
19+
long left = 0;
20+
long right = 0;
21+
22+
for(int time: times){
23+
if(right < time){
24+
right = time;
25+
}
26+
}
27+
28+
right = right * n;
29+
30+
answer = binary(left, right, times, n);
31+
32+
return answer;
33+
}
34+
35+
static long binary(long left, long right, int[] times, int n){
36+
37+
long result = 0;
38+
while(left < right){
39+
long mid = (left+right)/2;
40+
long sum = check(mid, times);
41+
42+
if(sum < n){
43+
left = mid+1;
44+
}else{
45+
result = mid;
46+
right = mid;
47+
}
48+
}
49+
50+
return result;
51+
}
52+
53+
static long check(long total, int []times){
54+
long sum = 0;
55+
56+
for(int time : times){
57+
sum += total/time;
58+
}
59+
60+
return sum;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)