Judge
Programmers - H-Index
깡구_
2022. 6. 24. 11:19
title: Programmers - H-Index
date: 2022-06-22
tags:
- Algorithm
https://programmers.co.kr/learn/courses/30/lessons/42747
문제 요약
한 과학자가 발표한 논문 중 h개 이상의 논문이 h번 이상 인용될 때, h의 최대값이 H-Index이다. H-Index를 구해 return한다.
문제 풀이
처음에는 이진 탐색을 이용하였는데 아무도 틀려보지 못한 항목들에서 틀렸다. 테스트 케이스를 찾을 수 없어 결국 다른 방식으로 풀었다.
인용 횟수가 아닌 논문의 개수에 집중하였다. 아무리 값이 크더라도 결국 논문의 개수가 충족되어야 하고, 인용 횟수가 주어졌으므로 논문의 개수를 통해 푸는 것이 훨씬 빠르다.
프로그램
#include <vector>
#include <algorithm>
using std :: vector ;
int solution ( vector < int > citations )
{
int answer = 0 ;
int * ipTemp ;
int i = 0 ;
int iTotal = citations.size () ;
int iLeft = 0 ;
int iRight = iTotal - 1 ;
int iMid = iRight / 2 ;
int iCnt = 0 ;
std :: sort ( citations.begin () , citations.end () ) ;
ipTemp = citations.data () ;
for ( int i = iRight ; i >= 0 ; --i )
{
if ( ipTemp [ i ] > answer )
++ answer ;
else
break ;
}
// while ( iLeft < iRight )
// {
// int iTemp = ipTemp [ iMid ] ;
//
//
// if ( iTemp <= ( iTotal - iMid ) ) // Can be H-Index
// {
// answer = iTemp ;
//
// iLeft = iMid + 1 ;
// iMid = ( iLeft + iRight ) / 2 ;
//
// continue ;
// }
//
// iRight = iMid - 1 ;
// iMid = ( iLeft + iMid ) / 2 ;
// }
//
// answer = std :: max ( answer , iTotal - iMid - 1 ) ;
//
// if ( answer < ipTemp [ 0 ] )
// ++ answer ;
return answer ;
}