Python sort & dictionary


  • 0
    class Solution(object):
        def hIndex(self, citations):
            citations.sort()
            for i in xrange(len(citations)):
                if citations[i] >= len(citations) - i:
                    return len(citations) - i
            return 0
    
    class Solution(object):
        def hIndex(self, citations):      
            dic = {}
            for c in citations:
                if c > len(citations):
                    dic[len(citations)] = dic.get(len(citations), 0 ) + 1
                else:
                    dic[c] = dic.get(c, 0) + 1
            cnt = 0
            for i in xrange(len(citations), -1, -1):
                cnt += dic.get(i, 0)
                if i <= cnt:
                    return i
            return 0
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.