原题链接在这里: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 citations each."

Example:

Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
  Since the researcher has 3 papers with at least 3 citations each and the remaining
  two with no more than 3 citations each, her h-index is 3.

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?

题解:

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.

最新文章

  1. blocking and unblocking mechanism for linux drivern code
  2. 布局包含Image和Title的UIButton
  3. 前端-SEO
  4. delphi图形图像开发相关
  5. css实现超连接按钮形式显示
  6. &lt;runtime&gt; 的 &lt;assemblyBinding&gt; 元素
  7. yaf for ubuntu安装
  8. javap -c命令详解
  9. (5)HomeAssistant mqtt-433-esp8266-arduino-传感器
  10. [程序员的业余生活]一周读完《高效能人士的七个习惯》Day1:这是不是一碗鸡汤?
  11. easyui 自动动态合并单元格
  12. VUE 图片验证码
  13. 内存泄漏 tensorflow
  14. 100-days: eighteen
  15. 使用JQuery进行DOM操作
  16. android拨号
  17. Spring Boot以War包启动
  18. Structs复习 简单数据校验
  19. openfire连接数据库mysql
  20. Jquery操作样式

热门文章

  1. HTTP请求中的User-Agent 判断浏览器类型的各种方法 网络爬虫的请求标示
  2. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到
  3. Html - Iframe
  4. 通过Sysprep封装系统
  5. java对象比较器和克隆
  6. ptmalloc2源码解析初探
  7. boolalpha
  8. 让你的PHP更安全之PHP.ini
  9. mysql 将null转代为0
  10. github中non-fast-forward错误的解决