728x90
추가적인 설명이 필요하면 댓글을 남겨주세요.
문제 설명
H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다.
어떤 과학자가 발표한 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값이 이 과학자의 H-Index입니다.
어떤 과학자가 발표한 논문의 인용 횟수를 담은 배열 citations가 매개변수로 주어질 때, 이 과학자의 H-Index를 return 하도록 solution 함수를 작성해주세요.
제한사항
- 과학자가 발표한 논문의 수는 1편 이상 1,000편 이하입니다.
- 논문별 인용 횟수는 0회 이상 10,000회 이하입니다.
Solution
1. 내림차순 정렬
2. 제일 작은 값이 citation.size()보다 큰 경우 그냥 그 사이즈값을 Return
3. answer을 처음에 citation.size()로 설정하고 하나씩 감소시키면서 조건에 맞는지 확인
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool comp(int a, int b){
return a > b;
}
int solution(vector<int> citations) {
int answer = citations.size();
int citation_before;
sort(citations.begin(), citations.end(), comp);
if (citations.back()>=answer) return answer;
else {
citation_before = citations.back();
citations.pop_back();
answer--;
while (true){
if((citations.back()>=answer)&&(citation_before <= answer)) return answer;
citation_before = citations.back();
citations.pop_back();
answer--;
}
}
return answer;
}
728x90
'알고리즘' 카테고리의 다른 글
[프로그래머스] 디스크 컨트롤러 C++ (heap) (0) | 2021.11.21 |
---|---|
[프로그래머스] 더 맵게 C++ (0) | 2021.11.21 |
[프로그래머스] 가장 큰 수 c++ (0) | 2021.08.23 |
[프로그래머스] 주식가격 c++ (0) | 2021.08.20 |
[프로그래머스] 다리를 지나는 트럭 c++ (0) | 2021.08.15 |