Java O(n) Hash Table solution (Didn't find similar idea in other posts)

  • 0

    I use the fact that the h-index won't decrease with more paper published: scan through the citations and maintain the h-index calculated from the scanned citations so far, mean while having a hash table to keep track of the counts of papers that are having the same citation that contributed to the current h-index.

    public class Solution {
        public int hIndex(int[] citations) {
            Map<Integer, Integer> map = new HashMap<>();
            int h = 0;
            for (int c : citations) {
                if (c > h) {
                    if (map.getOrDefault(h, 0) != 0) {
                        map.put(h, map.getOrDefault(h, 0) - 1);
                    } else {
                    map.put(c, map.getOrDefault(c, 0) + 1);
            return h;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.