Question
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Hint:
Expected runtime complexity is in O(log n) and the input is sorted.
Hide Tags Binary Search
Hide Similar Problems (M) H-Index
Solution
Get idea from here.
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if citations==None or len(citations)==0:
return 0
n = len(citations)
l, r = 0, n-1
while l<=r:
m = (l+r)/2
if citations[m]==n-m:
return n-m
elif citations[m]<n-m:
l = m+1
else:
r = m-1
return n-l