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
         if len(citations) == 0 or citations is None:
return 0
citations = sorted(citations, reverse=True)
#print ('cita: ', citations)
for i, c in enumerate(citations):
if i+1 > c:
return i
elif i+1 == c:
return i+1
return len(citations)
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
 
  if len(citations) == 0 or citations is None:
return 0
dicCount = {}
for ci in citations:
if ci > len(citations):
if len(citations) not in dicCount:
dicCount[len(citations)] = 1
else:
dicCount[len(citations)] += 1
else:
if ci not in dicCount:
dicCount[ci] = 1
else:
dicCount[ci] += 1
#print (' dicCount : ', dicCount)
sum = 0
for i in range(len(citations), -1, -1):
if i in dicCount:
sum += dicCount[i]
if sum >= i:
return i
 

[Leetcode] Sort, Hash -- 274. H-Index的更多相关文章

  1. Java实现 LeetCode 274 H指数

    274. H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高引用次数"(hig ...

  2. LeetCode—-Sort List

    LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...

  3. Leetcode 274.H指数

    H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...

  4. [LeetCode] H-Index II 求H指数之二

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  5. [LeetCode] 880. Decoded String at Index 在位置坐标处解码字符串

    An encoded string S is given.  To find and write the decoded string to a tape, the encoded string is ...

  6. [LeetCode] 275. H-Index II H指数 II

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  7. 【LeetCode】Hash

    [451] Sort Characters By Frequency [Medium] 给一个字符串,要求返回按照字母出现频率的排序后的字符串.(哈希表+桶排) 有个技巧是Hash用Value作为In ...

  8. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  9. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

随机推荐

  1. java集合的操作(set,Iterator)

    集合的操作 Iterator.Collection.Set和HashSet关系 Iterator<——Collection<——Set<——HashSet Iterator中的方法: ...

  2. 开通阿里云 CDN

    CDN,内容分发网络,主要功能是在不同的地点缓存内容,通过负载均衡技术,将用户的请求定向到最合适的缓存服务器上去获取内容,从而加快文件加载速度. 阿里云提供了按量计费的CDN,开启十分方便,于是我在自 ...

  3. 关于Integer与int

    integer a=new integer(1); integer b=new integer(1); int c=1; integer d=1; a==b  false因为地址不同: a==c  t ...

  4. hibernate 插入数据时让数据库默认值生效

    用hibernate做数据库插入操作时,在数据库端已经设置了对应列的默认值,但插入的数据一直为null.查找资料发现,原来是hibernate的配置项在作怪. Hibernate允许我们在映射文件里控 ...

  5. 详解Session分布式共享(.NET CORE版)

    一.前言&回顾 在上篇文章Session分布式共享 = Session + Redis + Nginx中,好多同学留言问了我好多问题,其中印象深刻的有:nginx挂了怎么办?采用Redis的S ...

  6. CI Weekly #18 | flow.ci iOS 最佳实践出炉,正式支持 Git@OSC 构建

    如大家所期待,flow.ci 现已支持开源中国的代码仓库 - 码云,可以直接构建 Git@OSC 的项目了,点击创建项目-选择代码仓库-选择码云-绑定 OSChina 账户-选择要构建项目,教程看这里 ...

  7. 如何使用MySQL触发器trigger

    阅读目录:触发器trigger的使用 创建触发器 单一执行语句.多执行语句 new.old详解 查看触发器 删除触发器:慎用触发器,不用就删除 Q:什么是触发器? A: 触发器是与表有关的数据库对象, ...

  8. DIV+CSS 规范命名集合

    一: 命名规范说明: 1).所有的命名最好都小写 2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",id="d ...

  9. centOS的命令行与图形页面之间的转换

    .命令行 -> 图形界面 注意:在安装CentOS7的时候要添加GUI图形界面,否则没有效果. # startx

  10. Vbs脚本实现radmin终极后门

    Vbs脚本实现radmin终极后门 代码如下: on error resume next const HKEY_LOCAL_MACHINE = &H80000002 strComputer = ...