[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
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)
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的更多相关文章
- Java实现 LeetCode 274 H指数
274. H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高引用次数"(hig ...
- LeetCode—-Sort List
LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...
- Leetcode 274.H指数
H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...
- [LeetCode] H-Index II 求H指数之二
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- [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 ...
- [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 ...
- 【LeetCode】Hash
[451] Sort Characters By Frequency [Medium] 给一个字符串,要求返回按照字母出现频率的排序后的字符串.(哈希表+桶排) 有个技巧是Hash用Value作为In ...
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
随机推荐
- nginx与apache配合反向代理技术2
注意,上次我们只是简单的在同一台服务器模拟搭建了一个新的http服务器(启用了8080端口),使用的是apache,从而模拟了多台服务器实现的Nginx反向代理,通过Nginx向上游代理服务器发送请求 ...
- 面向对象设计(Object-Oriented Design,OOD)
前言 OOD简介 Shubho:亲爱的,让我们开始学习OOD吧.你了解面向对象原则吗? Farhana:你是说封装,继承,多态对吗?我知道的. Shubho:好,我希望你已了解如何使用类和对象.今天我 ...
- 使用bootstrap的插件实现模态框效果
在上一篇文章中,我们使用 js+css 实现了模态框效果,在理解了模态框的基本实现方法和实现效果后,我们就要寻找更快捷的方法,又快又好的来完成模态框开发需求,从而节约时间,提高效率.一个好的轮子,不仅 ...
- mysql varchar类型转换int类型找出最大值
(1) 不严谨的,最简单的 select MAX(字段名 + 0) from 表名; (2) 使用函数实现 select MAX(cast(字段名 as SIGNED INTEGER)) from 表 ...
- 转:CentOS---网络配置详解
一.配置文件详解在RHEL或者CentOS等Redhat系的Linux系统里,跟网络有关的主要设置文件如下: /etc/host.conf 配置域名服务客户端的控制文件/etc/hos ...
- openMRS项目
openMRS项目的背景:我们的世界继续遭受大规模的大流行,因为超过4000万人感染或死于艾滋病毒/艾滋病-大多数(高达95%)是在发展中国家.预防和治疗艾滋病毒/艾滋病这种规模需要有效的信息管理,这 ...
- 企业账号打包如何通过HTML页面打开
企业账号打包后 Plist文件加入前缀 href="itms-services://?action=download-manifest&url= Plist文件在服务器中的地址为:h ...
- VUE依赖webpack分别给开发环境和生产环境配置不同的常量值并在项目中动态引用
当在开发和产品上线的时候,我们经常会遇到在同一个地方由于环境的不同而地址也不同的情况,这时候如果在代码中将该地址写死,那势必会造成上线时手动改动,多人开发及多处使用该地址难以维护等一系列问题,为避免这 ...
- 关于蓝桥杯嵌入式STM32的一点收获
各社团成员培训时自己总结的一点材料,直接粘过来的,可能有些朋友看不明白,总之这个比赛吧:有很多技巧,掌握到技巧能省我们半个月时间,我说的是针对这个比赛,对于STM32还是要多多练习,总之STM32还是 ...
- 001---Hibernate简介( 开源O/R映射框架)
该系列教程是使用hibernate3,hibernate4和3有区别(集成spring,使用等),请注意 001---Hibernate简介(开源O/R映射框架) ORM(Object Relatio ...