LeetCode H-Index II
原题链接在这里:https://leetcode.com/problems/h-index-ii/
题目:
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?
题解:
Have l = 0, r = n - 1, continue binary search with l <= r, if (nums[mid] == n - mid), we find the target.
Otherwise, when nums[mid] < n - mid, that means we guess smaller, l = mid+ 1. Else, r = mid - 1.
After gettting out of binary search and there is no result, return n - l.
Note: here it is n - l, not n - 1.
Time Complexity: O(logn).
Space: O(1).
AC Java:
public class Solution {
public int hIndex(int[] citations) {
if(citations == null || citations.length == 0){
return 0;
}
int len = citations.length;
int r = len-1;
int l = 0;
while(l<=r){
int mid = l+(r-l)/2;
if(citations[mid] == len-mid){
return len-mid;
}else if(citations[mid] < len-mid){
l = mid+1;
}else{
r = mid-1;
}
}
return len-l;
}
}
类似H-Index.
LeetCode H-Index II的更多相关文章
- Leetcode之二分法专题-275. H指数 II(H-Index II)
Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...
- Java实现 LeetCode 275 H指数 II
275. H指数 II 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高 ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵
H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用
[LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...
- [LeetCode] 275. H-Index II H指数 II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
随机推荐
- C语言根据日期(年,月,日)判断星期几(使用基姆拉尔森计算公式)
C语言根据日期判断星期几(使用基姆拉尔森计算公式) 算法如下: 基姆拉尔森计算公式W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 在公式中d表示日期中的日数, ...
- 用MyEclipse搭建SSH框架(Struts2 Spring Hibernate)
1.new一个web project. 2.右键项目,为项目添加Struts支持. 点击Finish.src目录下多了struts.xml配置文件. 3.使用MyEclipse DataBase Ex ...
- WPF 傻瓜生成 .dbml文件,以及文件用途原理是什么
- 使用SEP禁止客户端修改IP地址 - 飞舞的菜刀 - 51CTO技术博客
1.首先从注册表中搜寻 IP地址所在的位置,查到几项,此项即可: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Paramete ...
- 6. Configure Compute services
Controller Node: 1. sudo apt-get install nova-api nova-cert nova-conductor nova-consoleauth nova-nov ...
- 2. Basic environment configuration
网卡设置: Controller Node # The loopback network interface auto lo iface lo inet loopback # The primar ...
- 在Windows2003上安装Active Directory Management Gateway Service
为了让基于Windows2003的域控能够被Powershell管理,必须安装KB968934,但是直接安装会报以下的错误,必须先安装NDP35SP1-KB969166.但是"因为基于 Wi ...
- SonarQube代码质量管理平台安装与使用
Sonar简介 Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从七个维度检测代码质量 通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaS ...
- NBUT 1635 Explosion(最小顶点覆盖)
[1635] Explosion 时间限制: 10000 ms 内存限制: 65535 K 问题描述 there is a country which contains n cities connec ...
- 如何用JavaScript在页面上显示一个时间钟表
<html><head><script type="text/javascript">function myTime(){var today=n ...