[프로그래머스 문제풀이] 17. H-Index

WIGWAG·2023년 1월 4일
0

프로그래머스

목록 보기
17/32

처음에는 정렬하고 난 후 find_if로 이터레이터를 구하였지만 실패했다.

인덱스를 +1씩 올리면서 조건에 맞는 원소의 개수를 확인하면 답이 나오는데
내림차순으로 정렬하고 처음 원소부터 차례대로 인덱스의 크기와 비교한다면
인덱스의 크기가 더 커지는 시점에서 조건에 부합하는 원소의 개수를 얻을 수 있다.


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

#include <iostream>

using namespace std;

int solution(vector<int> citations) {
    sort(citations.begin(), citations.end(), greater<int>());

    for (int i = 0; i < citations.size(); ++i) {
        if (citations[i] < i + 1) {
            return i;
        }
    }

    return citations.size();
}

int main()
{
    cout << solution({ 3, 0, 6, 1, 5 }) << endl;
}

실행결과

3


H-Index 문제 링크

profile
윅왁의 프로그래밍 개발노트

0개의 댓글