[Leetcode] Sort, Hash -- 274. H-Index
Posted 安新
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode] Sort, Hash -- 274. H-Index相关的知识,希望对你有一定的参考价值。
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:
- My idea is to sort the array in descending order; find the index c[i] < i+ 1
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的主要内容,如果未能解决你的问题,请参考以下文章