안녕 세상아,

[프로그래머/c++] Lv2 H-index 본문

프로그래머스

[프로그래머/c++] Lv2 H-index

돈 많은 백수가 되고싶다 2024. 10. 6. 15:45

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

 

프로그래머스

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

programmers.co.kr

문제 자체는 쉬운 편이라고 생각.

처음 H-index 의미 잘 이해가 안돼서 위키피디아 정독했다..그러니까 간단하게 풀 수 있었음.

 

1. 내림차순으로 정렬한다. 

2. for문을 돌리면서 만약 i+1보다 현재 값인 citations[i]가 더 크거나 같으면 정답.

 

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

using namespace std;

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

    sort(citations.begin(), citations.end(), greater<>());

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

    return answer;
}