标签:nlogn exit method array sed ide desc main ace
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher‘s h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N ? h papers have no more than h citations each."
For example, given citations = [3, 0, 6, 1, 5]
, which means the researcher has 5
papers in total and each of them had received 3, 0, 6, 1, 5
citations respectively. Since the researcher has 3
papers with at least 3
citations each and the remaining two with no more than 3
citations each, his h-index is 3
.
Solution:
1 if len(citations) == 0 or citations is None: 2 return 0 3 citations = sorted(citations, reverse=True) 4 #print (‘cita: ‘, citations) 5 for i, c in enumerate(citations): 6 if i+1 > c: 7 return i 8 elif i+1 == c: 9 return i+1 10 return len(citations)
1 if len(citations) == 0 or citations is None: 2 return 0 3 dicCount = {} 4 for ci in citations: 5 if ci > len(citations): 6 if len(citations) not in dicCount: 7 dicCount[len(citations)] = 1 8 else: 9 dicCount[len(citations)] += 1 10 else: 11 if ci not in dicCount: 12 dicCount[ci] = 1 13 else: 14 dicCount[ci] += 1 15 #print (‘ dicCount : ‘, dicCount) 16 sum = 0 17 for i in range(len(citations), -1, -1): 18 if i in dicCount: 19 sum += dicCount[i] 20 if sum >= i: 21 return i
[Leetcode] Sort, Hash -- 274. H-Index
标签:nlogn exit method array sed ide desc main ace
原文地址:http://www.cnblogs.com/anxin6699/p/6977816.html