275. H-Index II
题目:
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Hint:
- Expected runtime complexity is in O(log n) and the input is sorted.
链接: http://leetcode.com/problems/h-index-ii/
题解:
假如给定数组是排序好的,求H-Index。 我们就可以用Binary Search。 target = len - mid。
Time Complexity - O(logn), Space Complexity - O(1)
public class Solution {
public int hIndex(int[] citations) {
if(citations == null || citations.length == 0) {
return 0;
}
int len = citations.length, lo = 0, hi = len - 1;
while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(citations[mid] == len - mid) {
return citations[mid];
} else if(citations[mid] > (len - mid)) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return len - lo;
}
}
题外话: 房子的装修师傅今天离开了,但是关键的电炉灶没接好,原因是总闸处220v电线有问题,导致插座没电。不过都是熟人,我也不想他们碰处一些危险的东西。虽然还要另找电工检查线路和接线,很费事费钱,但我想明年争取换份好工作,挣回来就好了。
二刷:
对于在位置i的论文来说, 至少len - i的论文, citations数目都比位置i的论文大。所以i位置的h-index至少是len - i。我们就可以用Binary Search来搜索答案。
Java:
public class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
int len = citations.length, lo = 0, hi = len - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (citations[mid] == len - mid) {
return citations[mid];
} else if (citations[mid] < len - mid) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return len - lo;
}
}
Reference:
https://leetcode.com/discuss/56122/standard-binary-search
https://leetcode.com/discuss/56170/java-binary-search-simple-and-clean
275. 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 代表"高 ...
- Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵
H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...
- 275 H-Index II H指数 II
这是 H指数 进阶问题:如果citations 是升序的会怎样?你可以优化你的算法吗? 详见:https://leetcode.com/problems/h-index-ii/description/ ...
- [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 ...
- leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)
https://leetcode.com/problems/h-index/ Given an array of citations (each citation is a non-negative ...
- [Swift]LeetCode275. H指数 II | H-Index II
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a ...
- Lintcode: Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- HDU-6278-Jsut$h$-index(主席树)
链接: https://vjudge.net/problem/HDU-6278 题意: The h-index of an author is the largest h where he has a ...
随机推荐
- JavaScript显示输出
记得c语言里的printf和java里的println吗,那么在JavaScript中怎么实现同样的功能呢 window.onload = function() { var para = docume ...
- Linux学习之路--启动VNC服务
我的Linux是Fedora 13,安装方法如下: 1.打开终端,执行 # yum install -y tigervnc tigervnc-server 2.编辑/etc/sysconfi/vncs ...
- Kibana4学习<二>
生产环境部署 Kibana4 是是一个完整的 web 应用.使用时,你需要做的只是打开浏览器,然后输入你运行 Kibana 的机器地址然后加上端口号.比如说:localhost:5601 或者 htt ...
- HTML5中的Canvas精品教程
http://javascript.ruanyifeng.com/htmlapi/canvas.html
- android开发 缩放到指定比例的尺寸
一种通过matrix矩阵缩放: //使用Bitmap加Matrix来缩放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) ...
- float和CGFloat混用的风险
一般意义上的混用是没有问题的, 比如 float x=5.0; (void)printNumber:(CGFloat)number; 当调用printNumber:x的时候是没有问题的 但是如果使用f ...
- JS--传统事件模型的问题
事件绑定分为两种情况:传统的事件绑定(内联模型.脚本模型),一种是现代事件绑定模型(DOM2级事件绑定). 内联模型的事件绑定是将事件写在元素标签中,将事件绑定函数当做元素的一种属性来实现的,这种绑定 ...
- 【BZOJ】【2002】【HNOI2010】弹飞绵羊
呃这题的Hint写着splay启发式合并……但是蒟蒻不懂T_T只好写个简单的LCT来蒙混过关,就是时间效率上差劲的很…… 不过能够一次AC心情也是蛮愉悦的~ /******************** ...
- 用c语言产生随机数的方法
用c语言产生随机数的方法 在C语言中,rand()函数可以用来产生随机数,但是这不是真正意义上的随机数,是一个伪随机数,是根据一个数,我们可以称它为种子,为基准以某个递推公式推算出来的一系数,当这系列 ...
- mysql 存储过程事务
DECLARE t_error INTEGER DEFAULT ; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=; START TRAN ...