Leetcode 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
.
Note: If there are several possible values for h
, the maximum one is taken as the h-index.
思路: 先将citations排序,假设case1: [0, 1, 3, 4, 5, 5, 5],这时正好有4篇引用数至少为4的文章,所以H-index就是4。这一题实际上是在search第一个不满足条件( (n - i) > citat[i] )
如果case2: [0, 1, 3, 5, 5, 5, 5],那么第一个不满足条件的位置是 citat[3] = 5,因为引用数大于等于5的文章只有n-3 = 4篇。这时的H-index就是n-3 = 4。
再例如case3: [100, 100],第一个不满足条件的位置是0,因为显然没有100篇文章被至少引用了100次。所以H-index = n - 0 = 2
注意L21,因为如果发生了case2或者case3,最后的 right 会由于left = right时不满足条件而运行L21,导致right跑到left左边去。所以最后停在第一个不满足条件的index上的是left。
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations:
return 0 n = len(citations)
left = 0
right = n - 1 while left <= right:
i = left + (right - left)/2
if citations[i] == n-i:
return n - i
elif citations[i] < n - i:
left = i + 1
else:
right = i - 1 return n - left
Leetcode H-index的更多相关文章
- [LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...
- LeetCode Minimum Index Sum of Two Lists
原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy a ...
- [LeetCode] Peak Index in a Mountain Array 山形数组的顶峰坐标
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- HDU-6278-Jsut$h$-index(主席树)
链接: https://vjudge.net/problem/HDU-6278 题意: The h-index of an author is the largest h where he has a ...
- [LeetCode] 274. H-Index H指数
Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...
- [LeetCode] 218. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- jQuery—一些常见方法(1)【filter(),not(),has(),next(),prev(),find(),eq(),index(),attr(),】
1.filter()和not()方法 filter()和not()是一对反方法,filter()是过滤. filter()方法是针对元素自身.(跟has()方法有区别) <script type ...
- FFmpeg的H.264解码器源代码简单分析:熵解码(Entropy Decoding)部分
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
随机推荐
- SS命令和Netstat命令比较
在早期运维工作中,查看服务器连接数一般都会用netstat命令.其实,有一个命令比netstat更高效,那就是ss(Socket Statistics)命令!ss命令可以用来获取socket统计信息, ...
- Java多态:upcast和downcast
upcast例: public class Test { public static void main(String[] args) { Cup aCup = new BrokenCup(); aC ...
- docker中如何制作自己的基础镜像
一.本地镜像 举个例子:现在把自己的开发环境打包,取名为centos6-base.tar,然后在docker中,以centos6-base.tar作为基准镜像. 1.创建自己的镜像,放置于/root目 ...
- Nginx1.9.0的安装
下载文件 http://nginx.org/en/download.html 下载 nginx-1.9.3.tar.gz 安装Nginx 安装 一.安装nginx时必须先安装相应的编译工具 yum - ...
- 假设检验:p-value,FDR,q-value
来源:http://blog.sina.com.cn/s/blog_6b1c9ed50101l02a.html,http://wenku.baidu.com/link?url=3mRTbARl0uPH ...
- Firefox使用svg blur滤镜渲染图片
很久没来更新博客了,今天正好比较闲,就写一篇手头项目上遇到的一个css问题: .mature .blur { -webkit-filter:blur(25px); -moz-filter:blur(2 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 组织机构的名称编号是否允许重复?
通常情况下,一个公司内部的部门名称,编号是不可能重复的.但是是在多公司的情况下,很可能有部门名称重复的问题存在,这时需要允许部门名称重复. 例如一个大型IT公司,在2个地区都有研发部或者客户服务部,这 ...
- 设置word里的代码格式,使之有底纹的效果
目录 1 实现效果: 1 2 怎么才能在word里实现这样的显示? 1 如何设置word里的代码格式,使之有底纹的效果 2 实现效果: 怎么才能在word里实现这 ...
- img加载在IE11,chrome,FF下的不同
IE11 img.complete 得不到img的大小,会使用img.onload chrome,ff:img.complete 得不到img的大小,会使用自己创建的img加载方法
- 用opencv的traincascade训练检测器
#1,准备正负样本 正样本:可以一张图片上多个sample,也可以一张图片单独成一个sample,准备多个sample.生成描述文件如下所示: 负样本:只要不含正样本,任意图片都可以作为负样本,但是最 ...