zoukankan      html  css  js  c++  java
  • 0275. H-Index II (M)

    题目

    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 h 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?

    题意

    设一个研究者共有n篇论文,如果其中有h篇,这h篇中每一篇都被引用过至少h次,而剩余的n-h篇中每一篇被引用的次数都不超过h,则称h为这个研究者的h指数。给定一个研究者n篇论文引用次数的升序数组,求该研究者h指数的最大值。

    思路

    0274. H-Index 相比,已经将数组按照升序排列,这样反而更简单。直接在 0274 的基础上使用二分查找进行改进即可。


    代码实现

    Java

    class Solution {
        public int hIndex(int[] citations) {
            int left = 0, right = citations.length - 1;
            while (left < right) {
                int mid = (right - left) / 2 + left;
                if (citations[mid] >= citations.length - mid) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
    
            return left == right && citations[left] >= citations.length - left ? citations.length - left : 0;
        }
    }
    
  • 相关阅读:
    ntp时间服务器
    locate 命令
    身份验证器
    centos 6 mysql源码安装
    简单上传下载命令lrzsz
    iptables记录日志
    iptables日志探秘
    du 命令
    Codeforces 1097F Alex and a TV Show (莫比乌斯反演)
    线段树教做人系列(1)HDU4967 Handling the Past
  • 原文地址:https://www.cnblogs.com/mapoos/p/13190991.html
Copyright © 2011-2022 走看看