최대 1 분 소요

Question

Q


Solution

  • citations에 대한 내림차순 정렬을 한다.
  • citations[0] 는 가장 큰 수 이므로 보다 같거나 큰 숫자를 citations 내에 몇개 있는지 찾는다.
  • citations[0]를 -1씩 감소 시키며 H-Index 조건에 부합하는 것을 찾는다.

Cord

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

using namespace std;

// citations 내에 h 보다 큰 숫자가 몇개 있는지 반환
int getHowMany(vector<int>& citations, int h)
{
    int cnt = 0;
    for (int i = 0; i < citations.size(); ++i)
    {
        if (h <= citations[i])
        {
            ++cnt;
        }
    }

    return cnt;
}

int solution(vector<int> citations)
{
    int answer = 0;

    // 내림차순 정렬
    sort(citations.begin(), citations.end(), greater<int>());

    // 최댓값부터 1씩 감소하여 H-Index 찾기
    int max = citations[0];
    int loop = 0;
    while (max - loop > 0)
    {
        int hIdx = max - loop;
        if (hIdx <= getHowMany(citations, hIdx))
        {
            answer = hIdx;
            break;
        }

        ++loop;
    }

    return answer;
}

Result

Result