H-Index

 

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

해결방안

 

구현

 

 


 

처음 시도한 코드 - 테스트 케이스 1개 통과 못함

 

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

int solution(vector<int> citations) {
    int h = 0; //h: H-Index
    int n = citations.size(); //n: 발표한 논문의 수
    sort(citations.begin(), citations.end());

    for (int i = 0; i < n; i++)
    {
        int hHigh = n - i - 1; // i번째 논문 이상 인용이 된 논문 수
        int hLow = i + 1;  // i번째 논문 이하 인용이 된 논문 수
        int paper = citations[i];

        if (hHigh <= hLow) break;

        if (hHigh >= paper && i<n-1) {
            if(citations[i] < citations[i+1])
                h = hHigh;
        }
    }

    return h;
}

 


 

 

코드

 

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

int solution(vector<int> citations) {
    int h = 0; //h: H-Index
    int n = citations.size(); //n: 발표한 논문의 수
    sort(citations.rbegin(), citations.rend());//높은값 순서로 정렬, 인용 횟수가 많은 논문부터 검사

    for (int i = 0; i < n; i++) 
    {
    	//현재 논문의 인용 횟수가 현재까지 검사한 논문 수보다 많다면
        if (citations[i] > i) //i번째 논문의 인용 횟수 > i번째 논문의 인용 횟수 이상의 논문들 수 i
            h = i + 1; //H-index는 '인용된 횟수 이상'인 논문의 개수이므로, 여기서는 검사한 논문 수(i+1)을 새로운 H-index로 설정
        else
            break; // 현재 논문의 인용 횟수가 현재까지 검사한 논문 수와 같거나 작아진 경우, 반복을 종료
    }

    return h;
}