题目:

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Hint:

  1. 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的更多相关文章

  1. Leetcode之二分法专题-275. H指数 II(H-Index II)

    Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...

  2. Java实现 LeetCode 275 H指数 II

    275. H指数 II 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高 ...

  3. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  4. 275 H-Index II H指数 II

    这是 H指数 进阶问题:如果citations 是升序的会怎样?你可以优化你的算法吗? 详见:https://leetcode.com/problems/h-index-ii/description/ ...

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

  6. 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 ...

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

  8. Lintcode: Permutation Index II

    Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  10. 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 ...

随机推荐

  1. opencv颜色识别代码分享

    android 平台 opencv 实现颜色识别代码:http://www.eyesourcecode.com/thread-40682-1-1.htmlopencv的颜色识别简单实现的代码:http ...

  2. InputStream和OutputStream与String之间的转换

    //1.字符串转inputstream String str="aaaaa"; InputStream in = new ByteArrayInputStream(str.getB ...

  3. 【收藏】Myeclipse优化

    1 .关闭MyEclipse的自动validation       windows > perferences > myeclipse > validation       将Bui ...

  4. Github的使用以及Git的简单入门 - 课程作业三

    GitHub创建项目 登录GitHub,在个人主页创建项目(repository) 创建后会生成2个文件,README.md和.gitignore.如图 创建本地仓库 如果是第一次使用git的话,需要 ...

  5. 一、javaSE总结

    1.变量与常量区别: 常量:是在程序中的不会变化的数据. 变量:其实就是内存中的一个存储空间,用于储存常量数据 ,方便运算,因为有些数据不确定,变量空间可以重复使用. 2.变量空间的开辟条件:数据类型 ...

  6. Html5 常见的新增API详解

    1. getElementsByClassName()方法 getElementsByClassName()方法接收一个参数,即一个包含一或多个类名的字符串,返回带有指定类的所有元素的NodeList ...

  7. Analyzer使用第二Y轴,以及同一分析图不同量值使用不同的图形样式

    Analyzer的建立分析图后,图中有两个量值,希望能显示成不同的图形样式,如一个是柱图.一个是线图. 1.设置显示多个量值: 3.设置显示出图例,即表明图中量值内容的说明: 2.右键图例中要修改为不 ...

  8. VIMTUTOR《VIM教程》

    =============================================================================== =      欢     迎     阅 ...

  9. 文件和文件夹同步工具AFiles 1.0 发布

    文件和文件夹同步工具AFiles 1.0  正式发布了! 本软件支持按文件日期或长度的各种比较方式来同步文件或者文件夹. 支持双向同步功能. 支持深层文件夹功能. 可以自动产生比较和同步的记录情况. ...

  10. sqlite3中的数据类型

    大多数的数据库引擎(到现在据我们所知的除了sqlite的每个sql数据库引擎)都使用静态的.刚性的类型,使用静态类型,数据的类型就由它的容器决定,这个容器是这个指被存放的特定列. Sqlite使用一个 ...