HomeLeetcode274. H-Index - Leetcode Solutions

274. H-Index – Leetcode Solutions

Description

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.

Examples:

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.

Example 2:

Input: citations = [1,3,1]
Output: 1

Solution in Python

Python
class Solution:
    def hIndex(self, citations: List[int]) -> int:
        # First, sort the list in descending order. This helps us easily check
        # how many papers have at least 'h' citations.
        citations.sort(reverse=True)
        
        # Initialize the h-index to 0
        h_index = 0
        
        # Iterate over the sorted citations list
        for i, citation in enumerate(citations):
            # 'i' represents the current index (0-based), and 'citation' is the
            # number of citations for that paper.
            
            # The condition for h-index is that at least 'h' papers have 'h' or
            # more citations. Here, 'i + 1' is the count of papers considered
            # (since the index starts from 0), and 'citation' is how many citations
            # the current paper has.
            
            # If the number of citations is greater than or equal to the number
            # of papers considered (i + 1), we update the h-index.
            if citation >= i + 1:
                h_index = i + 1
            else:
                # As soon as we find a paper with fewer citations than the number
                # of papers considered, we can stop the loop because the list is
                # sorted in descending order, and any further papers will also
                # have fewer citations.
                break
        
        # Return the calculated h-index
        return h_index

Explanation:

  1. Sorting: We start by sorting the citations array in descending order. This way, the most cited papers are evaluated first, and we can simply check how many papers have at least h citations.
  2. Iterating through the sorted list: We loop through the sorted list, and for each paper, we check if the number of citations is greater than or equal to the number of papers considered so far (i + 1 where i is the index). If so, we update the h_index.
  3. Early break: If we find a paper that has fewer citations than the number of papers considered, we break out of the loop. Since the list is sorted in descending order, no further papers will satisfy the condition.
  4. Return the h-index: Finally, we return the h_index after completing the loop.

Solution in C++

C++
class Solution {
public:
    int hIndex(vector<int>& citations) {
        // First, sort the citations array in descending order.
        // This makes it easier to find how many papers have at least 'h' citations.
        sort(citations.begin(), citations.end(), greater<int>());
        
        // Initialize h-index to 0
        int h_index = 0;

        // Iterate through the sorted list of citations
        for (int i = 0; i < citations.size(); ++i) {
            // 'i' is the current index, representing the number of papers considered (0-based index).
            // citations[i] is the number of citations for the current paper.
            
            // The h-index condition: we need at least 'h' papers with 'h' or more citations.
            // Here 'i + 1' is the number of papers considered so far (because index is 0-based),
            // and citations[i] is how many citations the current paper has.
            if (citations[i] >= i + 1) {
                // If the current paper has enough citations (>= i + 1), update the h-index.
                h_index = i + 1;
            } else {
                // If the current paper has fewer citations than the number of papers considered,
                // stop the loop since further papers will have even fewer citations (due to sorting).
                break;
            }
        }

        // Return the final h-index value
        return h_index;
    }
};

Solution in Javascript

JavaScript
/**
 * @param {number[]} citations
 * @return {number}
 */
var hIndex = function(citations) {
    // Step 1: Sort the citations array in descending order.
    // This makes it easier to count how many papers have at least 'h' citations.
    citations.sort((a, b) => b - a);
    
    // Step 2: Initialize the h-index to 0.
    let hIndex = 0;
    
    // Step 3: Loop through the sorted citations array.
    for (let i = 0; i < citations.length; i++) {
        // 'i' is the current index (0-based), representing the number of papers considered.
        // citations[i] is the number of citations for the current paper.
        
        // The condition for h-index is that at least 'h' papers have 'h' or more citations.
        // Here, 'i + 1' represents the number of papers considered so far (since the index is 0-based).
        if (citations[i] >= i + 1) {
            // If the current paper has at least 'i + 1' citations, update the h-index.
            hIndex = i + 1;
        } else {
            // If the current paper has fewer citations than the number of papers considered,
            // we stop the loop as all subsequent papers will have fewer citations.
            break;
        }
    }
    
    // Step 4: Return the calculated h-index.
    return hIndex;
};

Solution in Java

Java
import java.util.Arrays;

class Solution {
    public int hIndex(int[] citations) {
        // Step 1: Sort the citations array in descending order.
        // This helps us easily check how many papers have at least 'h' citations.
        Arrays.sort(citations);
        
        // Step 2: Initialize h-index to 0.
        int hIndex = 0;
        
        // Step 3: Loop through the sorted array from the end (highest citation first).
        // Since the array is sorted in ascending order, we traverse from the last element.
        for (int i = 0; i < citations.length; i++) {
            // The current paper's citations are citations[citations.length - 1 - i].
            int citation = citations[citations.length - 1 - i];
            
            // The condition for h-index: at least 'i + 1' papers have 'i + 1' or more citations.
            // Here, 'i + 1' represents the number of papers considered so far.
            if (citation >= i + 1) {
                // If the paper's citations are greater than or equal to 'i + 1', update h-index.
                hIndex = i + 1;
            } else {
                // If the current paper has fewer citations than the number of papers considered,
                // stop the loop as all remaining papers will have fewer citations.
                break;
            }
        }
        
        // Step 4: Return the calculated h-index.
        return hIndex;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular