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

 

코딩테스트 연습 - H-Index

H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다. 어떤 과학자가 발표

programmers.co.kr

코드 설명

문제의 이해가 푸는 시간보다 훨씬 더걸렸다....

문제 예시가 [3,0,6,1,5] 이다. 이를 인덱스와 비교해보면 (정렬 후)

0 6

1 5

2 3

3 1

4 0

순서로 인덱스가 더 커지지 않는 2까지 count하여 답은 3이다.

 

하나 더 생각해야 할점은 다음과 같이 구현하였을 때 [1,1,0,0,0] 케이스를 생각해보면 count와 원소값이 같다면 바로 리턴시켜주어야 한다.

코드

 

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

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

 

+ Recent posts