forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh-index-ii.py
More file actions
35 lines (27 loc) · 1.03 KB
/
Copy pathh-index-ii.py
File metadata and controls
35 lines (27 loc) · 1.03 KB
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
32
33
34
35
"""
Time: O(LogN)
Space: O(1)
Since citations is sorted,
i = N-1, if 1<=citations[i], it means that at least 1 of the citations is larger than 1. h-index is 1.
i = N-2, if 2<=citations[i], it means that at least 2 of the citations is larger than 2. h-index is 2.
...
i = 0, if N<=citations[i], it means that at least N of the citations is larger than N. h-index is N.
We can iterate from N-1 to 0. See what h-index ends up. Using O(N) of time.
We can also binary search the i, see which i match the condition.
"""
class Solution(object):
def hIndex(self, citations):
N = len(citations)
l = 0
r = N-1
while l<r:
i = (l+r)/2
h = N-i
if citations[i]>=h:
#h may be the h-index, check larger h.
r = i
else:
#h is not h-index, check smaller h.
l = i+1
#now, l is equal to r
return N-l if citations[l]!=0 else 0 #take care of edge case [0], [0, 0] or [0, 0, 0]