class Solution:
def hIndex(self, citations: List[int]) -> int:
# time O(NlgN) space O(1)
# main idea: first sort and compare citations[i] > i , find max i to meet the condition. hence k = i+1
citations.sort()# here if reverse will slow down the speed
size = len(citations)
i = 0
while i < size and i < citations[size-1-i]:
i += 1
return i
optimization using bucket sort
class Solution:
def hIndex(self, citations: List[int]) -> int:
# time O(N) space O(N)
# main idea: first sort and compare citations[i] > i , find max i to meet the condition. hence k = i+1
citations.sort()# here if reverse will slow down the speed
size = len(citations)
l = [0] * (size+1)
for c in citations:
if c >= size:
l[size] += 1
else:
l[c] += 1
cnt = 0
for i in range(size,-1,-1):
print(i)
cnt += l[i]
if cnt >= i:
return i # h == i here
return 0