H-Index

medium array counting sort

Problem

Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return compute the researcher's h-index. According to the definition of h-index on Wikipedia: A scientist has an index h if h of their n papers have at least h citations each, and the other n − h papers have no more than h citations each. If there are several possible values for h, the maximum one is taken as the h-index.

Inputcitations = [3, 0, 6, 1, 5]
Output3
3 papers have ≥ 3 citations (the ones with 3, 6, 5). No 4 papers have ≥ 4 citations. So h = 3.

def h_index(citations):
    n = len(citations)
    buckets = [0] * (n + 1)
    for c in citations:
        buckets[min(c, n)] += 1
    count = 0
    for h in range(n, -1, -1):
        count += buckets[h]
        if count >= h:
            return h
    return 0
function hIndex(citations) {
  const n = citations.length;
  const buckets = new Array(n + 1).fill(0);
  for (const c of citations) buckets[Math.min(c, n)]++;
  let count = 0;
  for (let h = n; h >= 0; h--) {
    count += buckets[h];
    if (count >= h) return h;
  }
  return 0;
}
class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int[] buckets = new int[n + 1];
        for (int c : citations) buckets[Math.min(c, n)]++;
        int count = 0;
        for (int h = n; h >= 0; h--) {
            count += buckets[h];
            if (count >= h) return h;
        }
        return 0;
    }
}
int hIndex(vector<int>& citations) {
    int n = citations.size();
    vector<int> buckets(n + 1, 0);
    for (int c : citations) buckets[min(c, n)]++;
    int count = 0;
    for (int h = n; h >= 0; h--) {
        count += buckets[h];
        if (count >= h) return h;
    }
    return 0;
}
Time: O(n) Space: O(n)