DEV Community

Debesh P.
Debesh P.

Posted on • Edited on

274. H-Index | LeetCode | Top Interview 150 | Coding Questions

Problem Link

https://leetcode.com/problems/h-index/


Detailed Step-by-Step Explanation

https://leetcode.com/problems/h-index/solutions/7439494/best-solution-in-the-world-most-optimal-2d7js


leetcode 274


Solution

class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int[] arr = new int[n+1];

        for(int i=0; i<n; i++) {
            if(citations[i] > n) {
                arr[n]++;
            }
            else {
                arr[citations[i]]++;
            }
        }

        int count = 0;
        for(int i=n; i>=0; i--) {
            count += arr[i];
            if(count >= i) {
                return i;
            }
        }

        return 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)