Intuition
My first thought was to sort the array, and return the first element where count < citations[i]. However this proved to be wrong, since this would return the first element, and after sorting the array we want to return the last element where count < citations[i] is True.
Approach
Sort the array, then iterate over it storing the count where count < citations[i].
Complexity
-
Time complexity:
O(n log n)since we have the sort function -
Space complexity:
O(1)
Code
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
citations = sorted(citations, reverse=True)
hIndex = 0
for i in range(len(citations)):
if (i+1) <= citations[i]:
hIndex = i+1
return hIndex