LeetCode 274: H-Index

Problem Description

Explanation

To solve this problem, we can sort the citations array in non-decreasing order. Then, we iterate through the sorted array and for each paper with citations >= index, we increment a counter. The h-index is the maximum value of h such that the researcher has published at least h papers that have each been cited at least h times.

  1. Sort the citations array in non-decreasing order.
  2. Initialize an h-index counter to 0.
  3. Iterate through the sorted array and for each paper with citations >= index, increment the counter.
  4. Return the final h-index.

Time complexity: O(n log n) to sort the array + O(n) to iterate through the sorted array = O(n log n) Space complexity: O(1)

Solutions

import java.util.Arrays;

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

Loading editor...