Finding H-index
Problem
Find h-index given array of numbers.For more information about h-index, check out the following link.
https://en.wikipedia.org/wiki/H-index
The idea is that you need to find the last value in the array where the value is larger than its index.
For example,
Say Scientist A has 5 publications with the following citation count.
Publications = {10, 8, 5, 4, 3}.
In this case, the h-index is 4 since 4 whose index is 3 is the last value which value is larger than its index.
Answer
Given the fact that you are searching the sorted values, we can use a binary search to find the h-index.
This algorithm will run in O( log N).
My first attempt was a little lousy and ran in O(N). It used the brute force search.
This is the code using binary search. We assume that the input is already sorted in descending order.
If not, the running time will be O( N log N) due to the sorting. We can use either quick sort or merge sort for this.
UPDATE (2019-07-31): solved the problem again in python using the binary search. Made silly mistakes that was found after running the code.
Practice statistics:
12:19: to write the correct algorithm for h-index
10:26: to write the code and test code (two bugs found after running. not returning the search result.)
UPDATE(2022-06-12): Solved the problem again. Took a while to understand what H-index is. Had a critical bug in the binary search to find the h-index.
Comments
Post a Comment