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

    H-Index (M)

    题目

    Given an array of citations (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 = [3,0,6,1,5]
    Output: 3 
    Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had 
                 received 3, 0, 6, 1, 5 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.


    题意

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

    思路

    h的范围很明显是 0-n,所以只要从n往回找到第一个满足的h,就是答案。因此可以这样处理:先将数组按升序排序,从头遍历数组,对于每一个i,length - i即为从i到最后一个元素的长度len,如果citations[i] >= len,说明后面len个论文的引用次数都大于等于len,且前面所有i个论文的引用次数必不大于len (证明如下),因此len就是要找的h指数。

    证明:假设在i处找到了满足的len,那么有 citations[i] > len (等于的情况无需证明),如果 citations[i - 1] > len,一定有 citations[i - 1] >= len + 1 (因为是整数),那么说明在i - 1处就应该已经找到了满足的len值,但实际情况并不是,所以 citation[i - 1] <= len,证明前i个论文的引用次数一定都不超过len,满足题目要求。

    也可以不用排序:记citations.length为n,建一个长度为n + 1的辅助数组aux,aux[i]表示引用次数等于i的论文篇数,aux[n]表示引用次数大于等于n的论文篇数。遍历citations,如果citations[i] > n,则aux[n]++,否则aux[citations[i]]++。再从后向前遍历aux数组,统计当前和是否大于等于当前下标index,是的话说明index就是所要求的的h指数。


    代码实现

    Java

    排序

    class Solution {
        public int hIndex(int[] citations) {
            Arrays.sort(citations);
            for (int i = 0; i < citations.length; i++) {
                if (citations[i] >= citations.length - i) {
                    return citations.length - i;
                }
            }
            return 0;
        }
    }
    

    不排序

    class Solution {
        public int hIndex(int[] citations) {
            int len = citations.length;
            int[] aux = new int[len + 1];
            for (int i = 0; i < len; i++) {
                if (citations[i] > len) {
                    aux[len]++;
                } else {
                    aux[citations[i]]++;
                }
            }
            int sum = 0;
            for (int i = aux.length - 1; i >= 0; i--) {
                sum += aux[i];
                if (sum >= i) {
                    return i;
                }
            }
            return 0;
        }
    }
    
  • 相关阅读:
    多测师讲解python _练习题002_高级讲师肖sir
    多测师讲解python _练习题002_高级讲师肖sir
    多测师讲解python _类(原始版)_高级讲师肖sir
    多测师讲解python _练习题003_高级讲师肖sir
    多测师讲解python _re模块_高级讲师肖sir
    多测师讲解python_os模块_高级讲师肖sir
    多测师讲解python _string_高级讲师肖sir
    MySQL介绍
    linux平台mysql密码设破解
    linux平台mysql密码设置
  • 原文地址:https://www.cnblogs.com/mapoos/p/13155614.html
Copyright © 2011-2022 走看看