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的更多相关文章

  1. [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 ...

  2. LeetCode Minimum Index Sum of Two Lists

    原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy a ...

  3. [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 ...

  4. 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 ...

  5. [LeetCode] 274. H-Index H指数

    Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...

  6. [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 ...

  7. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  8. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  9. jQuery—一些常见方法(1)【filter(),not(),has(),next(),prev(),find(),eq(),index(),attr(),】

    1.filter()和not()方法 filter()和not()是一对反方法,filter()是过滤. filter()方法是针对元素自身.(跟has()方法有区别) <script type ...

  10. FFmpeg的H.264解码器源代码简单分析:熵解码(Entropy Decoding)部分

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

随机推荐

  1. 多个mapper location时, mybatis spring的自动扫描配置

    1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔 2. SqlSessionFactoryBean里面的mapperLocations ...

  2. matlab:启动后无法调用工具包

    matlab有时候一打开会出现warning,然后任何工具包(包括set path)都执行不了,在网上找到解决方法,存下来备用. 1. 在matlab的command里输入: restoredefau ...

  3. 单页面网站关于id冲突的解决办法

    最近做了一个单页面的网站,所有的页面加载都是通过局部刷新的方式,并且不用iframe,并且我们引入了动态tab页签: 所有的页签里的内容都只是一个元素,都在同一个html页面上,没有任何iframe分 ...

  4. ssh 免密码设置失败原因总结

    先复习一下设置ssh免密码操作的步骤: 进入主目录 cd 生成公钥 ssh-keygen -t rsa -P '' (注:最后是二个单引号,表示不设置密码) 然后分发公钥到目标机器 ssh-copy- ...

  5. HoloLens开发手记 - Unity development overview 使用Unity开发概述

    Unity Technical Preview for HoloLens最新发行版为:Beta 24,发布于 09/07/2016 开始使用Unity开发HoloLens应用之前,确保你已经安装好了必 ...

  6. ASP.NET 小白从零开始建站简易教程 (一)域名、虚拟主机、FTP上传文件

    只考虑性价比,纯新手实验无备案.跟着步骤走半小时即可收获独立的个人网站一枚! 我的实验站 http://www.bearlab.site/ ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄ 目前总价花费86元(域名加虚 ...

  7. echarts .NET类库开源

    前言: 2012年从长沙跑到深圳,2016年又从深圳回到长沙,兜兜转转一圈,又回到了原点.4年在深圳就呆了一家公司,回长沙也是因为深圳公司无力为继,长沙股东老板挽留,想想自己年纪也不小了.就回来了,在 ...

  8. 领导让我重新做一个微信H5页面!

    leader:我们需要做一个微信H5页面,效果如图,功能如描述,时间越快越好. 需求是不是很简单呢?2015-11-24 12:44:00文末有最新更新 背景描述 前几天微信转发相关项目开发后,这是第 ...

  9. Ext.NET-布局篇

    概述 前一篇介绍了Ext.NET基础知识,并对Ext.NET布局进行了简要的说明,本文中我们用一个完整的示例代码来看看Ext.NET的布局. 示例代码下载地址>>>>> ...

  10. 没有jquery的时候,你看看这个

    vjs var br = (function() { var ua = navigator.userAgent.toLowerCase(); browser = { iPhone: /iphone/. ...