[Javascript] leetcode 274. h-index

준이·2025년 7월 8일

leetcode

목록 보기
3/3

문제

난이도: 중간 (Medium)

정수 배열 citations가 주어집니다.
citations[i]는 연구자의 i번째 논문이 받은 인용 수를 나타냅니다.

연구자의 H-Index를 반환하세요.

📌 H-Index란? (Wikipedia 기준 정의)
H-Index는 다음 조건을 만족하는 가장 큰 정수 h입니다:
연구자가 최소 h편의 논문을 가졌고,
그 각각이 h번 이상 인용되었을 때, 그 사람의 H-Index는 h입니다.

Example 1:

Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.
설명: 총 5편의 논문이 있고, 인용 수는 각각 [3, 0, 6, 1, 5]입니다.
이 중 최소 3편의 논문이 각각 3회 이상 인용되었고,
나머지 2편은 3회 이하 인용되었기 때문에,
H-Index는 3입니다.

Example 2:

Input: citations = [1,3,1]
Output: 1
설명: 1번 이상 인용된 논문이 2편 있지만,
2번 이상 인용된 논문은 1편뿐이므로,
H-Index는 1입니다.

Constraints(제약조건):

n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000

풀이

var hIndex = function(citations) {
    citations.sort((a, b) => b - a); // 내림차순 정렬

    for (let i = 0; i < citations.length; i++) {
        if (citations[i] < i + 1) {
            return i;
        }
    }

    return citations.length;
};

정렬을 기반으로한 조건탐색 문제로
핵심은

  1. sort((a,b) => b - a) -> 내림차순 정렬(큰 수부터)
  2. citations[i] >= i + 1 -> 갯수이기 때문에 i + 1을 해서 비교
profile
25% Speciallist

0개의 댓글