H-Index - Complete Solution Guide
H-Index is LeetCode problem 274, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an array of integers citations where citations[i] is the number of citations a researcher received for their i th 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. 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
Detailed Explanation
The H-Index problem asks us to determine a researcher's h-index given an array of citation counts for their papers. The h-index represents the maximum value 'h' such that the researcher has published at least 'h' papers with at least 'h' citations each. In simpler terms, we're looking for the largest number 'h' where at least 'h' papers have 'h' or more citations. The input is an array of integers representing citation counts, and the output is a single integer: the h-index. The constraints are: the array length (number of papers) can be up to 5000, and individual citation counts can be up to 1000.
Solution Approach
The solution uses a counting sort-like approach to efficiently determine the h-index. It creates a 'counts' array to store the number of papers with a specific citation count. It then iterates through the citations array, incrementing the corresponding count in the 'counts' array. After counting, it iterates backward from the maximum possible h-index (which is the total number of papers) down to 0. During this backward iteration, it accumulates the number of papers with at least 'h' citations. As soon as the accumulated count is greater than or equal to 'h', we've found our h-index, as there are at least 'h' papers with 'h' or more citations.
Step-by-Step Algorithm
- Step 1: Determine the number of papers (n) from the length of the citations array.
- Step 2: Initialize a 'counts' array of size n+1 with all elements set to 0. This array will store the frequency of papers having certain number of citations.
- Step 3: Iterate through the 'citations' array. For each citation count 'c': If 'c' is greater than or equal to 'n', increment counts[n]. Otherwise, increment counts[c]. The purpose is to count the number of papers with 'n' or more citations.
- Step 4: Initialize 'total_papers' to 0. This variable will track the number of papers with at least 'h' citations.
- Step 5: Iterate backward from 'h = n' down to 0. In each iteration:
- Step 6: Add counts[h] to 'total_papers'.
- Step 7: If 'total_papers' is greater than or equal to 'h', return 'h' because we have found the h-index.
- Step 8: If the loop completes without finding an h-index (total_papers never becomes >= h), return 0.
Key Insights
- Insight 1: Sorting the citations is a common but not necessary first approach. While it helps visualize the problem, the optimal solution doesn't rely on sorting the entire array.
- Insight 2: Counting sort or a similar counting-based approach allows for linear time complexity by avoiding full sorting. We leverage the constraint that the maximum citation is 1000.
- Insight 3: Iterating from the maximum possible h-index (n) down to 0 allows us to find the largest h-index that satisfies the condition by accumulating the count of papers with at least that many citations.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Sorting, Counting Sort.
Companies
Asked at: ByteDance, Nvidia, Zoox.