[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:

 

  1. My idea is to sort the array in descending order; find the index  c[i] < i+ 1
        iterate to compare the index  and value 
       if the value < index i+1, then stop and return the index as the final answer
       if value == index i,  return the index i+1
       else it is the length of the array
 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)
View Code
time complexity:  o(nlogn), space  complexity o(1)
 
2 nd method is use hashmap
time complexity o(n),  space complexity o(n)
get the array value count, when the value > len(array) L, count into the hashmap[L] 
 
--reference 
http://www.cnblogs.com/yrbbest/p/5031910.html
 
 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
View Code

 

 

以上是关于[Leetcode] Sort, Hash -- 274. H-Index的主要内容,如果未能解决你的问题,请参考以下文章

HDOJ-ACM1425 sort 简单hash应用

each sort delete

redis sort 实现list与hash的连接 查出sql表的一行

hash的排序(转载)

三色排序

[leetcode sort]75. Sort Colors