• [LeetCode#274]H-Index


    Problem:

    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."

    For example, given citations = [3, 0, 6, 1, 5], which 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, his h-index is 3.

    Note: If there are several possible values for h, the maximum one is taken as the h-index.

    Credits:
    Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

    Analysis:

    This problem is interesting!!! It tests your coding skill and logic ability.
    
    Since h-index defines that at least h papers should exceed(include) h citation, you may wrongly think this is a simple count problem, why not use HashMap<citation, count>. 
    However, even paper that have citation' larger than citation should be counted! What a pity, Right?
    Apparently the HashMap should not be used!!!
    
    Since there is good prperty : all papers have citation exceed certain index, could be counted for that index. Why not use sort??? Then, sorting the citation array in descending order, (i+1) is the total citations exceed citation[i]. 
    Then I have following implementations:

    Wrong solution 1:

    public class Solution {
        public int hIndex(int[] citations) {
            if (citations == null)
                throw new IllegalArgumentException("The citaions' reference is null!");
            Arrays.sort(citations, Collections.reverseOrder());
            for (int i = 0; i < citations.length; i++) {
                if (i+1 >= citations[i])
                    return citations[i];
            }
            return citations.length;
        }
    }
    Error:
    Line 5: error: no suitable method found for sort(int[],Comparator<Object>)
    Mistake 1:
    Arrays.sort(citations, Collections.reverseOrder()); 
    Not work for primitive type, it only works for Integer, Double ....

    Wrong solution 2:

    Since we should give up the way of sorting citations in descending order, we should just use ascending order. 
    For the citation in ascending order, citation[i]'s useful count is the number of papers after it (inclusive).
    for (int i = 0; i < citations.length; i++) {
        if (citations.length - i >= citations[i])
            ...
    }
    
    
    public class Solution {
        public int hIndex(int[] citations) {
            if (citations == null)
                throw new IllegalArgumentException("The citaions' reference is null!");
            Arrays.sort(citations);
            int max = -1;
            for (int i = 0; i < citations.length; i++) {
                if (citations.length - i >= citations[i])
                    max = Math.max(max, citations[i]);
            }
            return (max == -1 ? citations.length : max);
        }
    }
    
    Errors:
    Input:
    [4,4,0,0]
    Output:
    0
    Expected:
    2
    
    However, the above solution only consider the situation of "citations.length - i >= citations[i]" and no citation[i] is valid case (at the end).
    Even we may not be able to find citations[i] meet:
    if (citations.length - i >= citations[i])
        max = Math.max(max, citations[i]);
    
    We still should have a valid h-index!
    Suppose we have no "citations.length - i >= citations[i]" case, it means
    citations.length - i < citations[i]
    
    since "citations[i]"" < "citations[citaions.length - i]"(thus all citations[i] account into citations[citaions.length - i]).
    Thus we must have (possible)hindex = citations.length - i.

    Solution:

    public class Solution {
        public int hIndex(int[] citations) {
            if (citations == null)
                throw new IllegalArgumentException("The citaions' reference is null!");
            Arrays.sort(citations);
            int max = 0;
            for (int i = 0; i < citations.length; i++) {
                if (citations.length - i >= citations[i])
                    max = Math.max(max, citations[i]);
                else 
                    max = Math.max(max, citations.length - i);
            }
            return max;
        }
    }
  • 相关阅读:
    20145337《网络对抗技术》逆向及BOF基础
    20145337 《信息安全系统设计基础》课程总结
    微信小程序——3、逻辑js文件
    微信小程序——2、配置json文件
    微信小程序——1、文件的认识
    20145336 张子扬 《网络对抗技术》 web安全基础实践
    20145336 张子扬 《网络对抗技术》web基础
    20145336张子扬 《网络对抗技术》 网络欺诈技术防范
    20145336《网络对抗技术》Exp6 信息搜集技术
    20145336张子扬《网络对抗》MSF基础应用
  • 原文地址:https://www.cnblogs.com/airwindow/p/4781064.html
Copyright © 2020-2023  润新知