Algorithm

[Algorithm] 프로그래머스 C++ : H-Index

dokylee 2020. 8. 19. 16:59

 

 

https://programmers.co.kr/learn/courses/30/lessons/42747#

 

코딩테스트 연습 - H-Index

H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다. 어떤 과학자가 발표

programmers.co.kr

 

h가 citations의 배열에 있는 숫자가 아닐 수도 있다는 걸 놓쳐서 시간이 더 걸린 문제

 

 

일단 citations를 정렬하고

 

그냥 while&for문 브루트포스h가 0부터 1씩 늘어날 때마다 

 

h보다 큰 수가 몇개인지 cnt로 세고

 

h>cnt일때 break걸고 그때의 h값부터 안되는거니까

 

h-1이 답이 됨

 

 

Code

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int solution(vector<int> citations) {
    int answer = 0;
    int h = 0;
    
    sort(citations.begin(), citations.end());
    
    while(h<=citations.size()) {
        int cnt = 0;
        for(int n: citations){
            if(n>=h) cnt++;
        }
        
        if(cnt<h) break;
        h++;
    }

    // h일때 조건에 안맞아서 while문을 break 했으므로 -1해주기
    answer = h-1;
    
    return answer;
}