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 citations each."

Example:

Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 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, her h-index is 3.

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 citations is 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;
}
};

类似题目:

H-Index

参考资料:

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指数之二的更多相关文章

  1. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Lonely Pixel II 孤独的像素之二

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  5. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  6. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  7. [LeetCode] My Calendar II 我的日历之二

    Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...

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

  9. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

随机推荐

  1. WPF 后台数据触发改变界面状态-心跳实现

    今年做的一个上位机工控WPF项目,做个小小的总结把,以后随时来找 请不要带血乱喷,我只是菜鸟.___by 鲍队 类似于这样子的;大致的意思是:一个代码变量,通过改变变量的值,绑定这个变量的这个圆颜色也 ...

  2. [Android下载]北京新版小学英语三年级上册点读手机软件

    小学英语三年级上册点读软件.根据2014年北京教改版教材编写,发音标准.实现点读功能.点到哪里读到哪里.哪里不会点哪里!北京教育科学研究院编写,北京出版社出版.ISBN:9787200108781   ...

  3. Windows程序设计_19_测试Windows应用程序加载函数

    /* 本程序测试自定义的WinMainCRTStartup函数 */ #define STRICT #define WIN32_LEAN_AND_MEAN #include <windows.h ...

  4. 响应式手机商城页面顶部样式HTML代码

    本特效支持PC浏览器和触屏浏览器. 效果展示 http://hovertree.com/texiao/bootstrap/8/ 手机扫描二维码体验效果: HTML代码如下: <!DOCTYPE ...

  5. CSS3鼠标悬停图片上浮显示描述代码

    效果:http://hovertree.com/texiao/css3/20/ 效果图: 代码如下: <!doctype html> <html lang="zh" ...

  6. js通过循环多张图片实现动画效果

    以小鱼摇尾巴和眨眼睛为例 动画思路: 1.将图片资源放在数组里面 2.通过计时器来设定间隔时间 3.通过计数器来取相应的图片 第一步:基本框架,鱼身体 <body> <canvas ...

  7. JavaScript和jQuery的类型判断

    此博文为原创,转载请注明出处! 对于类型的判断,JavaScript用typeof来进行. 栗子: console.log(typeof null); //object console.log(typ ...

  8. SharePoint 2010/2013/2016内容数据库与网站集的关系

    总得来说,内容数据库和网站集的关系是: 一个内容数据库里可以有多个网站集,但是一个网站集只能存在于一个内容数据库. 那么问题来了 问题1:我能否在创建网站集时指定内容数据库呢?或者说我能在指定的内容数 ...

  9. Javascript前端和JAVA后端对加密库的处理实例

    前端加密 Javascript的加解密有开源的库,http://www.oschina.net/p/crypto-js/ 如下是具体的使用例子 <!DOCTYPE html> <ht ...

  10. android AsynTask处理返回数据和AsynTask使用get,post请求

    Android是一个单线程模型,Android界面(UI)的绘制都只能在主线程中进行,如果在主线程中进行耗时的操作,就会影响UI的绘制和事件的响应.所以在android规定,不可在主线中进行耗时操作, ...