[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 has5
papers in total and each of them had- received 0
, 1, 3, 5, 6
citations respectively.- Since the researcher has
3
papers with at least3
citations each and the remaining- two with no more than
3
citations 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
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;
- }
- };
类似题目:
参考资料:
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 ...
随机推荐
- SQL Server索引视图以(物化视图)及索引视图与查询重写
本位出处:http://www.cnblogs.com/wy123/p/6041122.html 经常听Oracle的同学说起来物化视图,物化视图的作用之一就是可以实现查询重写,听起来有一种高大上的感 ...
- 【十大经典数据挖掘算法】AdaBoost
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 集成学习 集成学习(ensem ...
- 基于HTML5实现3D热图Heatmap应用
Heatmap热图通过众多数据点信息,汇聚成直观可视化颜色效果,热图已广泛被应用于气象预报.医疗成像.机房温度监控等行业,甚至应用于竞技体育领域的数据分析. http://www.hightopo.c ...
- ASP.NET MVC传递参数(model)
一看到此标题,相信你也会.因为路由是可以从URL地址栏传过去的. 但是Insus.NET不想在地址栏传递,还是一个条件是jQuery的Ajax进行POST的.Insus.NET不清楚别人是怎样处理的, ...
- Entity Framework Extended Library
扩展了实体框架的功能类库. https://github.com/loresoft/EntityFramework.Extended 1批量更新/删除 1)删除 //delete all users ...
- 【挖财工作笔记】idea使用指南
一 安装破解 破解选择服务器,然后选择地址:http://www.iteblog.com/idea/key.php http://idea.iteblog.com/key.php http://i ...
- 子div设置浮动无法把父div撑开。
<div class="mainBox"> <div class="leftBox"></div> <div clas ...
- div 加载 html页面的方法
做网页的单页面应用时,需要在一个HTML的Div元素中加载另一个HTML页面,以前有一种方法就是用iframe,举例如下: <div class="main-container&quo ...
- wamp 服务器安装问题 及cmd常用命令 和 php mysql数据库常用cmd命令集
1 官网下载wamp软件包,根据提示安装 2 目录结构: wamp: bin/为套件目录 包括mysql apache php log 日志记录 alias 配置 apps 数据库 ...
- 如何采用easyui tree编写简单角色权限代码
首先每个管理员得对应一个角色: 而角色可以操作多个栏目,这种情况下我们可以采用tree多选的方式: 在页面上js代码: $('#Permission').dialog({ title: '栏目权限', ...