题目:

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. IOS常用加密DES

    NSString+DES.h // // NSString+DES.h // haochang // // Created by Administrator on 14-4-15. // Copyri ...

  2. ExtJS登陆页面涉及到的几个问题

    1.如何在文本框中增加提示信息      输入框中无法直接使用tooltip,需要使用单独的代码 var tip = Ext.create('Ext.tip.ToolTip', { target : ...

  3. Memcached常用命令及使用说明

    一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...

  4. 数据导出为excel表格

    ---恢复内容开始--- 方式一: 通过request和response中携带的数据导出表格,导出的结果会将页面中展示的内容全部导出.代码如下: //调出保存框,下载页面所有内容 String fil ...

  5. 怪物AI(复习)

    怪物AI执行 //-----------------------------------------------主动攻击---------------------------------------- ...

  6. A beginner’s introduction to Deep Learning

    A beginner’s introduction to Deep Learning I am Samvita from the Business Team of HyperVerge. I join ...

  7. aspx文件、aspx.cs文件、aspx.designer.cs文件之讲解

    .aspx文件:(页面)书写页面代码.存储的是页面design代码.只是放各个控件的代码,处理代码一般放在.cs文件中. .aspx.cs文件:(代码隐藏页)书写类代码.存储的是程序代码.一般存放与数 ...

  8. KIP-32 Add timestamps to Kafka message

    通过KIP32,Kafka的每条消息都加进了时间戳,这个KIP在0.10.0.0被加入. 说到“时间”,先贴张图,娱乐一下(如果对星球大战系列电影不熟的话,请自动略过……) 这个KIP的文档在 KIP ...

  9. Sqli-labs less 61

    Less-61 此处对于id处理还是有点奇葩的,第一次遇到利用两层括号的.(可能我头发比较长,见识短了).形式和上述是一样的 payload: http://127.0.0.1/sqli-labs/L ...

  10. 【一】php 操作符

    1.php单引号和双引号的区别 单引号和双引号都能表示字符串,但是单引号不能识别里面带有转义字符'/'和变量的字符串,所以需要""去表示这种字符串.或者使用<<< ...