## H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which 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, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

## Analysis:

This problem is pretty straightforward.The easist way which requires more time is sorting the array, the time complexity will be O(n log n) (we ignore this method in this post).

Another way is to use more space, but we can get O(n) time, which is faster than the sorting approach. Thinking about the definition of H-index, the max number of h is the number of paper we have. So we could use a map (or vector in my case), to store: if current paper has i citations, we add map[i] by 1. Note that if citation is more than the number of papers, we added 1 to the last index of the map.

In this way, for each key i in our map, we only need to add the sum of all values afer i to the end, we will know how many papers are there have more then i citations. The maximum i is what we want.

## Code (C++):

class Solution {
public:
int hIndex(vector<int>& citations) {
int ssum = 0;
int sz = citations.size();
vector<int>mp(sz+1,0);
for (int i=0;i<sz;++i){
mp[min(sz,citations[i])] +=1;
}
for (int i=sz;i>=0;--i){
ssum += mp[i];
if (ssum >= i){return i;}
}
return 0;
}
};

## Code (Python):

class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
ssum = 0
sz = len(citations)
mp = *(sz+1)
for ci in citations:
mp[min(sz,ci)] +=1
for i in range(sz,-1,-1):
ssum += mp[i]
if ssum>=i:
return i
return 0