[LeetCode] H-Index II 求H指数之二
Given an array of citations sorted in ascending order (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."
Example:
Input:citations = [0,1,3,5,6]
Output: 3
Explanation:[0,1,3,5,6]means the researcher has5papers in total and each of them had
received 0, 1, 3, 5, 6citations respectively.
Since the researcher has3papers with at least3citations each and the remaining
two with no more than3citations each, her h-index is3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
- This is a follow up problem to H-Index, where
citationsis now guaranteed to be sorted in ascending order. - Could you solve it in logarithmic time complexity?
这题是之前那道 H-Index 的拓展,输入数组是有序的,让我们在 O(log n) 的时间内完成计算,看到这个时间复杂度,而且数组又是有序的,应该有很敏锐的意识应该用二分查找法,属于博主之前的总结帖 LeetCode Binary Search Summary 二分搜索法小结 中的第五类,目标值 target 会随着 mid 值的变化而变化,这里的 right 的初始值和 while 循环条件是否加等号是需要注意的问题,一般来说,博主的习惯是把 right 初始化为数组的长度,然后循环条件中不加等号,但是这种 right 的初始化对于这种目标值不固定的情况下不好使,需要初始化为长度减1(目前博主还没有遇到反例,有的话请务必告知博主)。那么此时循环条件中是否要加等号,这个其实很玄学,在 Find Peak Element 中,right 也是初始化为数组长度减1,但是循环条件却不能加等号。这道题却一定需要加等号,否则会跪在 [0] 这个 test case,有些时候固有的规律并不好使,可能只能代一些 corner case 来进行检验,比如 [], [0], [1,2] 这种最简便的例子。
基于上面的分析,我们最先初始化 left 和 right 为0和 len-1,然后取中间值 mid,比较 citations[mid] 和 len-mid 做比较,如果前者大,则 right 移到 mid 之前,反之 right 移到 mid 之后,循环条件是 left<=right,最后返回 len-left 即可,参见代码如下:
class Solution {
public:
int hIndex(vector<int>& citations) {
int len = citations.size(), left = , right = len - ;
while (left <= right) {
int mid = 0.5 * (left + right);
if (citations[mid] == len - mid) return len - mid;
else if (citations[mid] > len - mid) right = mid - ;
else left = mid + ;
}
return len - left;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/h-index-ii/
https://leetcode.com/problems/h-index-ii/discuss/71063/Standard-binary-search
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] H-Index II 求H指数之二的更多相关文章
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element II 求大多数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- [LeetCode] My Calendar II 我的日历之二
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...
- [LeetCode] 454. 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
随机推荐
- Spring MVC中文文档翻译发布
前后经过九个月,我翻译的Spring MVC官方4.2.4版本中文文档可以发布第一个较为完整的版本了.译文上尽量做到准确并且符合中文习惯,让人能读懂,能理解.现全文发布如下,也希望它能够给出其价值,并 ...
- cin.ignore()函数的用法
cin.ignore(a,ch)方法是从输入流(cin)中提取字符,提取的字符被忽略(ignore),不被使用.每抛弃一个字符,它都要计数和比较字符:如果计数值达到a或者被抛弃的字符是ch,则cin. ...
- java.lang.Class.isPrimitive()用法解析
一.概述: 此方法主要用来判断Class是否为原始类型(boolean.char.byte.short.int.long.float.double). 二.格式: Class.isPrimitive( ...
- Java第三方数据库连接池库-DBCP-C3P0-Tomcat内置连接池
连接池原理 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接池 ...
- 用C#从数据库动态生成AdminLTE菜单的一种方法
当前的应用设计风格趋于Flat扁平化,很多基于BootStrap实现了很多UI非常漂亮的管理界面(Bootstrap admin template). 此核心文件开源在Github:https://g ...
- 【原】JAVA SE编码规范
/* * 编码规范: * 1.所有的命名遵循"见名知意"的原则 * 2.所有的命名不允许使用汉字或拼音 * 3.Java的工程命名建议使用小写,比如:oa.crm.cms... * ...
- JavaScript简单分页,兼容IE6,~3KB
简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...
- script在html中的摆放位置
以前一直觉得script在html中的任何位置都可以,今天做一个需求的时候才更正了自己的错误思想啊--script的位置也不是随便放的. 首先是想实现一个select标签,有是和无两个option,但 ...
- 如何使用VS在SharePont 2013中插入ashx文件
http://www.lifeonplanetgroove.com/adding-and-deploying-generic-handlers-ashx-to-a-sharepoint-2010-vi ...
- Visual Studio 生成事件命令
Visual Studio在生成项目工程前后,有时我们需要做一些特殊的操作,比如:拷贝生成的dll到指定目标下面等. 结合VS可以添加预先生成事件和后期生成事件,采用命令或bat批处理. 1.Visu ...