Problem
Given an array of integers citations
where citations[i]
is the number of citations a researcher received for their ith
paper, return compute the researcher’s h
-index.
According to the definition of h-index on Wikipedia: A scientist has an index h
if h
of their n
papers have at least h
citations each, and the other n − h
papers have no more than h
citations each.
If there are several possible values for h
, the maximum one is taken as the h
-index.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Sorting and track counts from start of array
Algorithm
- Sort the array of length
n
- Now, we iterate from left to right with iterator
i
, then for each indexi
,n - i
is number of citations on the right (inclusive of current element), which more than current citation. We have to find max such value, such that there exist a position in sorted array, thatcitation[i]
>=n - i
.
Dry Run
|
|
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- 🧺 Space complexity:
O(1)
Method 2 -Sorting and track counts from end of array
We can also do the same count from the end of array.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- 🧺 Space complexity:
O(1)
Dry Run
Dry running above code for [3,0,6,1,5]
:
|
|
Method 3 - Bucket Sort 🏆
We can also put the citations in a counting sort array, then iterate over the counter array.
First, you may ask why bucket sort. Well, the h-index is defined as the number of papers with reference greater than the number. So assume n
is the total number of papers, if we have n+1
buckets, number from 0 to n, then for any paper with reference corresponding to the index of the bucket, we increment the count for that bucket. The only exception is that for any paper with larger number of reference than n
, we put in the n
-th bucket.
Then we find total
which is the total number of papers that has more than i citations, and we stop when total>=i
(total number of papers with more than i citations >= i). We don’t need to keep going because we are trying the biggest i possible, we we stop and return the result.
Code
|
|
Dry Run
Dry running above code for [3,0,6,1,5]
:
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)
Method 4 - Sorting and Binary Search 🥈
We can improvise method 1. In method 1, we sort the array, and then try to find the point such that citation[i] >= (n - i)
using linear search. But array is sorted we can do binary search.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- 🧺 Space complexity:
O(1)