码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode #275 H-Index II

时间:2020-01-10 00:53:46      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:class   二分查找   maximum   bsp   integer   标准库   blank   不用   span   

Question

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.

二分查找O(logn)

这题表面上是承接了H-index的一道题,实际上是一道典型的Binary Search题。

当citations排好序之后,直接找出citations[i] < i的位置即可。由于这里上升序排序,所以实际写的时候和降序稍有不同。

但此题的关键不是想到用Binary Search,而是处理Binary Search那些复杂的细节。这里介绍C++标准库<algorithm>里的超简洁、bug free的通用写法:

def lower_bound(array, first, last, value): # 返回[first, last)内第一个不小于value的值的位置
    while first < last: # 搜索区间[first, last)不为空
        mid = first + (last - first) // 2  # 防溢出 
        if array[mid] < value: first = mid + 1 
        else: last = mid 
   return first # last也行,因为[first, last)为空的时候它们重合

这样写的好处很多:

1. 简洁好记,只有first = mid+1 处用到了位置调整

2. 即使区间为空、答案不存在、有重复元素、搜索开/闭的上/下界也同样适用,鲁棒性高

要点:

1. 搜索区间是左闭右开,[first, last)

2. 只有first = mid + 1,而last = mid (这能避免死循环,详见参考资料)

3.  mid = first + (last - first) // 2 (防溢出,适用于指针、迭代器,当然python大数可以不用考虑溢出)

 

参考:

https://www.zhihu.com/question/36132386/answer/530313852

LeetCode #275 H-Index II

标签:class   二分查找   maximum   bsp   integer   标准库   blank   不用   span   

原文地址:https://www.cnblogs.com/sbj123456789/p/12174113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!