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 ...
随机推荐
- 苹果IOS开发者账号的区别,企业账号,个人账号,公司团队账号,教育账号
苹果IOS开发者账号总结 详细地址:https://developer.apple.com/programs/which-program/ 个人账号(Individual): 费用99美金一年, 该账 ...
- TP框架自带的正则验证的规则(转载)
thinkphp框架里面自带有很多自动验证的规则,下面是框架自带的正则验证的规则,官方的说明文档里面没有这么多,所以记下来,以备使用. view sourceprint?01static $regex ...
- C#中使用Log4net日志输出到本地文件、Textbox或Listview
网上很多配置log4net的方法,但是排行靠前的 根本就没有说明清除,导致浪费了两个小时来搞清楚如何配置,真是无语,特写此文,给那些刚接触log4net的朋友 1.参考链接:http://blog.s ...
- Ice的HelloWorld(Java)
Ice是一种面向对象的中间间平台,入门ice,简单的HelloWorld是必不可少的. 转载请注明http://www.cnblogs.com/zrtqsk/p/3745286.html,谢谢. 一. ...
- Theano3.5-练习之深度卷积网络
来源:http://deeplearning.net/tutorial/lenet.html#lenet Convolutional Neural Networks (LeNet) note:这部分假 ...
- C#发展历程以及C#6.0新特性
一.C#发展历程 下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的. 二.C#6.0新特性 1.字符串插值 (String In ...
- Node进阶:核心模块http简介
本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. http模块概览 大多数nodejs开发者都是冲着开发web s ...
- hystrix-turbine 监控的使用
1. 概述 Demo地址:http://git.oschina.net/zhou666/spring-cloud-7simple/tree/master/cloud-hystrix-turbine ...
- RHEL每天定时备份Oracle
步骤: (1)创建脚本文件bak_112.sh,内容如下(自动按当前日期备份数据库): #!/bin/sh export ORACLE_BASE=/u01/app/oracle; ORACLE_HOM ...
- C语言文件的读写
对文件的读和写是最常用的文件操作.在C语言中提供了多种文件读写的函数: 字符读写函数 :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式 ...