LeetCode # 274. H-Index array

Description


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.



Ideas


Solution One

Purely violent problem solving. From the title, h <= N, so using a double-layer loop, the outer layer traverses all possible values ​​of h, and the inner layer traverses the citations to find the number of ≥ h and ≤ h.

Use two counters to count at least h and no more than h respectively, and use if + continue to ensure that each number is counted only once. Note that the counter cnt1 counts h numbers and no longer counts, to ensure that the calculator cnt2 can correctly count the remaining N-h papers with citations not exceeding h.

Since the calculator cnt1 counts h numbers and no longer counts, we need to sort in descending order in advance to avoid counting errors. For example, the count result of this set of data in [2, 1] should be the same as the set of data in [1, 2] of.

Time complexity: O (n ^ 2) = sorting O (nlgn) + two-layer loop O (n ^ 2)
space complexity: O (1)

185 ms, Memory 6.5 MB, ranking 5.56%

class Solution {
public:
    int hIndex(vector<int> &citations) {
        sort(citations.begin(), citations.end(), greater<int>());
        int paper_num = citations.size();
        int max_h_idx = 0;  // h index for a scientist must include 0

        for (int h = 1; h <= paper_num; ++h) {
            int cnt1 = 0;
            int cnt2 = 0;

            for (int cit : citations) {
                if (cnt1 != h && cit >= h) {
                    ++cnt1;
                    continue;  // guarantee only count every element once
                }
                
                if (cit <= h) {
                    ++cnt2;
                }
            }

            if (cnt1 == h && cnt2 == (paper_num - h)) {
                max_h_idx = h;
            }
        }

        return max_h_idx;
    }
};



Solution Two

Wikipedia gives an algorithm for statistical H-Index:

  • Sort all the SCI papers published by them from high to low citations;
  • Look for the sorted list from the back until the serial number of a certain paper is greater than the number of times the paper is cited. The resulting serial number minus one is the H index.

The principle I understand is: in this algorithm, the value of h is equal to the index value of the array. After sorting in descending order, each time the condition traverses an element, it means that at least one paper has citations ≥ h, and the remaining half of the array The elements happen to be papers with citations ≤ h. Therefore, when the index value i> = citations [i], it means that the number of citations of these papers in citations [0..i] is ≥ h, and the number of citations of these papers in citations [i..n] is ≤ h, which is just The answer to this algorithm question.

Time complexity: O (n) = sorting O (nlgn) + traversal citations O (n)
space complexity: O (1)

4 ms, Memory 6.6 MB, ranking 70.58%

class Solution {
public:
    int hIndex(vector<int> &citations) {
        sort(citations.begin(), citations.end(), greater<int>());

        for (int i = 0; i < citations.size(); ++i) {
            if (i >= citations[i]) return i;
        }

        return citations.size();
    }
};



reference




Guess you like

Origin www.cnblogs.com/Bw98blogs/p/12719937.html