Skip to content

Commit 28dcde3

Browse files
committed
O(nlogn) time and O(1) space using Sorting.
1 parent 7ae2dbb commit 28dcde3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

274. H-Index/274. H-Index.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
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.
3+
4+
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."
5+
6+
Example:
7+
8+
Input: citations = [3,0,6,1,5]
9+
Output: 3
10+
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
11+
received 3, 0, 6, 1, 5 citations respectively.
12+
Since the researcher has 3 papers with at least 3 citations each and the remaining
13+
two with no more than 3 citations each, her h-index is 3.
14+
Note: If there are several possible values for h, the maximum one is taken as the h-index.
15+
"""
16+
class Solution:
17+
def hIndex(self, citations: List[int]) -> int:
18+
citations.sort(reverse = True)
19+
result = 0
20+
for i in range(len(citations)):
21+
if citations[i] - 1 >= i:
22+
result += 1
23+
return result

0 commit comments

Comments
 (0)