Moments of growth

[이분 탐색] 입국 심사 본문

Algorithm [C++]/Programmers [Level3]

[이분 탐색] 입국 심사

뮤링이 2022. 6. 28. 01:33
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

long long solution(int n, vector<int> times) {
    long long answer = 0;
    sort(times.begin(), times.end());
    
    long long start = (long long)times[0];
    long long end = (long long)times[times.size()-1] * n; 
    
    while(start<=end){
        long long sum=0;
        long long mid = (start+end)/2;
        
        for(int i: times){
            sum+=mid/(long long)i;
        }
        
        if(sum>=n){ //더 낮춰도 됨
            if(answer == 0){
                answer = mid;
            }else{
                answer = min(answer,mid);
            }
            end = mid-1;
        }else{
            start = mid+1;
        }
    }
    return answer;
}

'Algorithm [C++] > Programmers [Level3]' 카테고리의 다른 글

[Heap] 디스크 컨트롤러  (0) 2022.05.23
[Heap] 이중우선순위큐  (0) 2022.05.16
Comments