[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 has3
papers with at least3
citations each and the remaining
two with no more than3
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 ...
随机推荐
- ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化
原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...
- Hadoop学习之旅一:Hello Hadoop
开篇概述 随着计算机网络基础设施的完善,社交网络和电商的发展以及物连网的推进,产生了越来越多的大数据,使得人工智能最近几年也有了长足的发展(可供机器学习的样本数据量足够大了),大数据的存储和处理也越来 ...
- Index
我主要在研究.NET/C# 实现 PC IMERP 和 Android IMERP ,目的在解决企业通信中遇到的各类自动化问题 分布式缓存框架: Microsoft Velocity:微软自家分布 ...
- WPF入门:数据绑定
上一篇我们将XAML大概做了个了解 ,这篇将继续学习WPF数据绑定的相关内容 数据源与控件的Binding Binding作为数据传送UI的通道,通过INotityPropertyChanged接口的 ...
- Entity Framework 教程——EF体系结构
EF体系结构 下图是一张EF体系结构的全景图,让我们单独了解各个组件的用处. EDM (Entity Data Model): EDM由3个主要部分组成,概念模块(Conceptual Model), ...
- VS2015突然报错————Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value 'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper
Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with ...
- JDBC 制作简单的登录验证
两种方法: 一.直接拼接到SQL语句 public static void main(String[] args) throws Exception{ //输入账号密码 Scanner sc = ne ...
- Hibernate框架之Criteria查询 和注解(重点☆☆☆☆☆,难点☆☆☆)
写好一篇博客,不是容易的事.原因是:你要给自己以后看的时候,还能看懂,最重要的是当别人看到你的博客文章的时候,也一样很清楚的明白你自己写的东西.其实这也是一种成就感!! 对于每一个知识点,要有必要的解 ...
- js生成一个不重复的ID的函数的进化之路
在MongoDB中的ObjectID,可以理解为是一个不会重复的ID,这里有个链接http://blog.csdn.net/xiamizy/article/details/41521025感兴趣可以去 ...
- 小程序https Android 安卓可以发request请求,IOS 苹果 发请求失败问题
如果一个机器可以发送成功,一个机器发送失败,那多半是是域名的https支持的问题 那就用腾讯云的这个ssl测试工具检测下 https://www.qcloud.com/product/ssl#user ...