넘치게 채우기

[LeetCode] 274. H-Index 본문

PS/LeetCode

[LeetCode] 274. H-Index

riveroverflow 2023. 7. 23. 16:29
728x90
반응형

https://leetcode.com/problems/h-index/description/?envType=study-plan-v2&envId=top-interview-150 

 

H-Index - LeetCode

Can you solve this real interview question? H-Index - Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher's h-index. According to the definition of h-index on W

leetcode.com

문제 유형 : 배열

문제 난이도 : Medium

 

문제

Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher's h-index.

According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.

 

각 문서의 인용 수가 담긴 배열 citations가 주어집니다. 이 연구자의 h지수를 구하시오.

h지수는 연구자의 최소 h번 인용된 h개의 문서 중 최고의 h값입니다.

 

 

풀이

배열을 정렬하면 문서의 수를 구하기 쉬워집니다. 수를 비교할 필요없이 단순하게 인덱스로만 구하면 됩니다.

 

배열을 내림차순으로 순회하면서 만약 인용수의 값 citations[i]가 i보다 작아지면, 순회를 멈추면 됩니다.

h지수 h는 i+1로 매번 갱신합니다.

 

코드(C++)

class Solution {
public:
    int hIndex(vector<int>& citations) {
        const int n = citations.size();
        sort(citations.begin(), citations.end(), greater<int>());

        int h = 0;
        for (int i = 0; i < n; ++i) {
            if (citations[i] >= i + 1) {
                h = i + 1;
            } else {
                break;
            }
        }

        return h;
    }
};
 
728x90
반응형

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] 380. Insert Delete GetRandom O(1)  (0) 2023.07.24
[LeetCode] 50. Pow(x, n)  (0) 2023.07.24
[LeetCode] 383. Ransom Note  (0) 2023.07.22
[LeetCode] 673. Number of Longest Increasing Subsequence  (0) 2023.07.21
[LeetCode] 45. Jump Game II  (0) 2023.07.12