본문 바로가기

problem solving

[프로그래머스]H-index

val번 이상 인용횟수 중 가장 큰 val을 찾으면 된다.

주어진 배열인 citations를 정렬하고,

val을 0부터 citations배열의 마지막까지 +1씩 늘려가면서

lower_bound를 이용해 val번 이상의 가장 최저점을 찾아 인용횟수를 구하고 조건에 부합하는지 검사한다.

조건에 맞다면 answer변수에 val을 넣어준다.

#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int solution(vector<int> citations) {
    int answer = 0;
    
    int val = 0;
    sort(citations.begin(), citations.end());
    while(val <= citations[citations.size() - 1]) {
        int pos = lower_bound(citations.begin(), citations.end(), val) - citations.begin();
        answer = citations.size() - pos >= val ? val : answer;
        val++;
    }
    
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]숫자 야구  (0) 2020.03.10
[프로그래머스]전화번호 목록  (0) 2020.03.10
[프로그래머스]더 맵게  (0) 2020.03.10
[프로그래머스]소수 찾기  (0) 2020.03.10
[프로그래머스]큰 수 만들기  (0) 2020.03.09