Study hard

(c++)프로그래머스 코딩테스트 연습 - H-Index 본문

프로그래머스/정렬

(c++)프로그래머스 코딩테스트 연습 - H-Index

Nimgnoej 2020. 10. 21. 23:40

programmers.co.kr/learn/courses/30/lessons/42747

 

코딩테스트 연습 - H-Index

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

programmers.co.kr

[풀이]

H-Index는 citations안에 있는 원소로 국한되는 것이 아니다.

하지만 H-Index가 될 수 있는 범위의 최댓값은 citations의 최댓값이다.

H-Index를 citations의 최댓값으로 초기화하고, 1씩 줄여가면서 조건에 맞는지 확인하였다.

 

#include <string>
#include <vector>
#include <algorithm>//sort, max

using namespace std;

bool sortDesc(int &A, int &B) {
	return A > B;
}

int solution(vector<int> citations) {
	int answer = 0;
	//내림차순 정렬
	sort(citations.begin(), citations.end(), sortDesc);
	//H-Index가 될 수 있는 최대값은 citations에서 가장 큰 값임
	int H_Index = citations[0];
	//모두 0이면 H-Index는 0
	if (H_Index == 0)
		return 0;
	int Max = -1;
	while (H_Index > 0) {
		for (int i = 0; i < citations.size(); i++) {
			//H_Index번 이상 인용됐고, 그 이상 인용된 논문 개수가 H_index개 이상일 때
			if (citations[i] >= H_Index && H_Index <= i + 1) {
				Max = max(Max, H_Index);
				continue;
			}
		}
		H_Index--;
	}
	return Max;
}