Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith 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 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, their h-index is 3.
Example 2:
Input: citations = [1,3,1]
Output: 1
Constraints:
n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000
Sort the list reversely, and then go through the list. Each time compare the previous h-index
with the min(paper_number, paper_citation)
. The sorting would include a time complexity of o ( n log n ) o(n\log n) o(nlogn), and iteration with the complexity of o ( n ) o(n) o(n).
Time complexity: o ( n log n ) o(n \log n) o(nlogn)
Space complexity: o ( n ) o(n) o(n)
We could reduce the time complexity by skipping the sort. Firstly we go through the citations
, and use a map to keep track of citation and the number of the papers that have this citation. The key is the citation, and the value is the number of the papers that have this citation. Then we iterate the map, from the largest citation to the smallest. Since the citations have a limit, we could iterate from the largest to the smallest without sorting.
Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)
class Solution:
def hIndex(self, citations: List[int]) -> int:
citations.sort(reverse=True)
res = 0
for i, each_citation in enumerate(citations):
res = max(res, min(i + 1, each_citation))
return res
class Solution:
def hIndex(self, citations: List[int]) -> int:
citation = {}
max_citation = 0
for each_citation in citations:
citation[each_citation] = citation.get(each_citation, 0) + 1
max_citation = max(max_citation, each_citation)
res = 0
paper_cnt = 0
for h_index in range(max_citation, -1, -1):
if h_index in citation:
paper_cnt += citation[h_index]
res = max(res, min(h_index, paper_cnt))
return res