blob: b8d1fda723268ae0eaa65214245450ff9e63cafd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# 274. H-Index
from itertools import accumulate
"""
given an array of integers 'citations' where 'citations[i]' is the number of
citations a researcher received for their i'th paper, return the researchers
h-index. according to the definition of h-index on wikipedia: the h-index is
given as the maximum value of 'h' such that the given researcher published at
least 'h' papers that have each been cited 'h' times.
"""
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
n = len(citations)
buckets = [0] * (n + 1)
for i in citations:
buckets[min(i, n)] += 1
accum = list(accumulate(buckets[1:][::-1]))[::-1]
compr = [accum[i] >= i + 1 for i in range(n)]
return (compr + [0]).index(0)
if __name__ == "__main__":
obj = Solution()
print(obj.hIndex([3, 0, 6, 1, 5]))
print(obj.hIndex([1, 3, 1]))
|