274. H-Index
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method 1: Sorting
This approach leverages sorting to simplify the problem. The h-index definition involves counts of papers and citation thresholds. If we sort the citations, we can systematically check for the h-index condition.
Solution
H-Index in Python
class Solution:
def hIndex(self, citations: List[int]) -> int:
n = len(citations)
# Create a buckets array to store the count of papers for each citation count.
# The size is n+1 because h-index can be at most n.
# Any paper with more than n citations is equivalent to a paper with n citations
# for the purpose of calculating an h-index up to n.
buckets = [0] * (n + 1)
for c in citations:
if c >= n:
# If citation count is n or more, group it in the last bucket.
buckets[n] += 1
else:
buckets[c] += 1
# Iterate from the highest possible h-index (n) downwards.
# Accumulate the counts to find the number of papers with at least i citations.
count = 0
for i in range(n, -1, -1):
count += buckets[i]
# If the number of papers (count) is greater than or equal to
# the citation count (i), we have found the h-index.
if count >= i:
return i
return 0 # Should not be reached given the problem constraints (n >= 1).
H-Index in Java
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
// Buckets array to store counts of papers for each citation number.
// Size is n+1. buckets[i] stores number of papers with i citations.
// Citations >= n are treated as n.
int[] buckets = new int[n + 1];
for (int c : citations) {
if (c >= n) {
buckets[n]++;
} else {
buckets[c]++;
}
}
// Iterate from n down to 0 to find the h-index.
// 'count' will be the number of papers with citations >= i.
int count = 0;
for (int i = n; i >= 0; i--) {
count += buckets[i];
// If count (papers with >= i citations) is >= i,
// then i is the h-index. Since we iterate downwards,
// the first such i we find is the maximum possible.
if (count >= i) {
return i;
}
}
return 0; // Should not be reached given problem constraints (n >= 1).
}
}
H-Index in C++
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
// Create a vector to act as buckets for counting citations.
// The size is n+1. buckets[i] stores the number of papers with i citations.
// Citations greater than or equal to n are grouped in buckets[n].
vector<int> buckets(n + 1, 0);
for (int c : citations) {
if (c >= n) {
buckets[n]++;
} else {
buckets[c]++;
}
}
// Iterate from n down to 0 to find the h-index.
// 'count' will accumulate the number of papers with citations >= i.
int count = 0;
for (int i = n; i >= 0; i--) {
count += buckets[i];
// If the total number of papers with at least i citations
// is greater than or equal to i, we have found the h-index.
// Since we iterate from high to low, the first one we find is the answer.
if (count >= i) {
return i;
}
}
return 0; // Should not be reached given problem constraints (n >= 1).
}
};
H-Index in TypeScript
function hIndex(citations: number[]): number {
const n = citations.length;
// Buckets array to store counts of papers for each citation number.
// Size is n+1. buckets[i] stores number of papers with i citations.
// Citations >= n are treated as n.
const buckets: number[] = new Array(n + 1).fill(0);
for (const c of citations) {
if (c >= n) {
buckets[n]++;
} else {
buckets[c]++;
}
}
// Iterate from n down to 0 to find the h-index.
// 'count' will be the number of papers with citations >= i.
let count = 0;
for (let i = n; i >= 0; i--) {
count += buckets[i];
// If count (papers with >= i citations) is >= i,
// then i is the h-index. Since we iterate downwards,
// the first such i we find is the maximum possible.
if (count >= i) {
return i;
}
}
return 0; // Should not be reached given problem constraints (n >= 1).
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.