본문 바로가기

Algorithm/프로그래머스

프로그래머스 H-Index C++

반응형

프로그래머스 : H-Index

 

프로그래머스

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

programmers.co.kr

 


- 문제

  h 번 인용된 논문의 최대값을 구하는 문제다.

 

 


- 풀이

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

using namespace std;

int solution(vector<int> citations) {
    int answer = 0;
    sort(citations.begin(),citations.end(),greater<int>());
    for(int i = 0; i < citations.size(); i++){
        if(citations[i] >= i + 1){
            answer++;
        }
        else{
            break;
        }
    }
    
    return answer;
}

 

 

내림차순으로 정렬을 하면 큰 숫자가 먼저 뜨니깐, 큰 순서대로 count를 하면서 이 count가 인용된 횟수보다 작아지는 순간까지 +1씩 하면 된다.

 

  


- 기억할 것!

 X

반응형