[프로그래머스] H-Index

hamsteak·2023년 10월 10일
0

ps

목록 보기
31/39

h번 이상 등장하는 h 이상의 값들 중 최댓값을 구하는 문제.

i번째 원소의 값보다 큰 값이 i-1개 만큼 있는지 확인하면 된다. 정렬을 이용하면 i번째보다 큰 값의 개수를 상수 시간 내에 구할 수 있다.

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

cpp code

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

using namespace std;

int solution(vector<int> citations) {
    sort(citations.begin(), citations.end(), less<int>());
    
    int h = 0;
    for (int i=0; i<citations.size(); i++) {
        h = max(h, min(citations[i], (int)citations.size() - i));
    }
    
    return h;
}
profile
안녕하세요

0개의 댓글