[프로그래머스] Level.2 정렬-H-Index

2021. 1. 15. 17:21Algorithm/프로그래머스

 

코딩테스트 연습 - H-Index

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

programmers.co.kr

 

문제설명
제한사항
입출력예

 

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> citations) {
	int answer = 0;
	int use,no,h_idx = 0;

	sort(citations.rbegin(), citations.rend());
    int h=citations[0];
    
    while(1){
        use=0,no=0;
        for (int i = 0;i < citations.size();i++) {
			if (h <= citations[i]) use++;
			if (citations[i] <= h) no++;
		}
		if ((h<=use&&no<=h)||h==0) {
			h_idx = h;
			break;
		}
        else h--;
	}
	answer = h_idx;
	return answer;
}