码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode] Sort, Hash -- 274. H-Index

时间:2017-06-10 10:42:23      阅读:208      评论:0      收藏:0      [点我收藏+]

标签: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. My idea is to sort the array in descending order
       then 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

标签:nlogn   exit   method   array   sed   ide   desc   main   ace   

原文地址:http://www.cnblogs.com/anxin6699/p/6977816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!