H-Index-leetcode

widgets Article
H-Index-leetcode

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.

Algorithm one, sort

hIndex is defined as that there are h articles, and the number of times each article is cited is at least h; and find the maximum value of h. It is hIndex.

In order to facilitate comparison, the descending order is used here.

class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.rbegin(), citations.rend());
int index = 0;
while (index < citations.size() && citations[index] > index)
++index;
return index;
}
};

Algorithm two, O (n)

Algorithm one needs to be sorted, so the time complexity is O (nlogn).

The value range of hIndex is [0, citataions.size ()].

Introduce additional storage space, count [i], the subscript indicates the number of citations of the article, and the array element value is the number of articles that have reached the citation count.

1. The first scan completes the count array statistics

2. The second pass scans the count array from back to front and accumulates from back to front to get the number of articles with citations exceeding the value of i. If the number of articles> = i, then the definition of hIndex is satisfied.

class Solution {
public:
int hIndex(vector<int>& citations) {
const int M = citations.size();
vector<int> count(M+2);
for (auto c: citations)
++count[c>M ? M : c];
for (int i=M; i>=0; i--) {
count[i] += count[i+1];
if (count[i] >= i)
return i;
}
return 0;
}
};

Algorithm three, partition

Use the quick sorting method.

1. Select a pivot and aggregate the value less than the pivot to the left, and the value greater than or equal to the right.

2. If the width of the right section is less than or equal to the pivot, then the hIndex value is at least the width of the right section.

At this time, continue to divide again in the left section to find the largest hIndex value

3. If the width of the right section> pivot, the definition of hIndex is not met. Continue to divide in the right section.

4. Until the interval is empty.

The actual execution time on the leetcode is 4ms.

class Solution {
public:
int hIndex(vector<int>& citations) {
const int M = citations.size();
int hindex = 0;
int start = 0, stop = M-1;
while (start <= stop) {
auto pivot = partition(citations, start, stop);
if (M - pivot <= citations[pivot]) {
hindex = M - pivot;
stop = pivot - 1;
}
else
start = pivot + 1;
}
return hindex;
}

int partition(vector<int>& citations, int start, int stop) {
const auto pivot = citations[stop];
for (auto i=start; i<stop; i++) {
if (citations[i] < pivot)
swap(citations[i], citations[start++]);
}
swap(citations[start], citations[stop]);
return start;
}
};