Skip to content

Commit ad47fb8

Browse files
committed
Update h-index.py
1 parent 64b94e0 commit ad47fb8

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

Python/h-index.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Time: O(nlogn)
2-
# Space: O(1)
1+
# Time: O(n)
2+
# Space: O(n)
33

44
# Given an array of citations (each citation is a non-negative integer)
55
# of a researcher, write a function to compute the researcher's h-index.
@@ -19,6 +19,29 @@
1919
#
2020

2121
class Solution(object):
22+
def hIndex(self, citations):
23+
"""
24+
:type citations: List[int]
25+
:rtype: int
26+
"""
27+
n = len(citations);
28+
count = [0] * (n + 1)
29+
for x in citations:
30+
if x >= n:
31+
count[n] += 1
32+
else:
33+
count[x] += 1
34+
35+
h = 0
36+
for i in reversed(xrange(0, n + 1)):
37+
h += count[i]
38+
if h >= i:
39+
return i
40+
return h
41+
42+
# Time: O(nlogn)
43+
# Space: O(1)
44+
class Solution2(object):
2245
def hIndex(self, citations):
2346
"""
2447
:type citations: List[int]

0 commit comments

Comments
 (0)