Algorithm


Problem Name: 275. H-Index II

Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in an ascending order, 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.

You must write an algorithm that runs in logarithmic time.

 

Example 1:

Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 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, their h-index is 3.

Example 2:

Input: citations = [1,2,100]
Output: 2

 

Constraints:

  • n == citations.length
  • 1 <= n <= 105
  • 0 <= citations[i] <= 1000
  • citations is sorted in ascending order.

Code Examples

#1 Code Example with C Programming

Code - C Programming


int hIndex(int* citations, int citationsSize) {
    int i, h = 0;
    for (i = 0; i  <  citationsSize; i ++) {
        if (citations[citationsSize - 1 - i] >= i + 1) {
            h = i + 1;
        } else {
            break;
        }
    }
    return h;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
[0,1,3,5,6]

Output

x
+
cmd
3

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int hIndex(vector<int>& citations) {
        int i = 0, j = citations.size() - 1;
        while(j >= 0 && citations[j] > i) i++, j--;
        return i;
    }
};

// Binary Search, O(logn)
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int lo = 0, len = citations.size(), hi = len - 1;
        int mid = lo + (hi - lo) / 2;
        while(lo  < = hi){
            if(citations[mid] >= len - mid) hi = mid - 1;
            else lo = mid + 1;
            mid = lo + (hi - lo) / 2;
        }
        return len - lo;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
[0,1,3,5,6]

Output

x
+
cmd
3

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int hIndex(int[] citations) {
        if (citations.length == 0) {
            return 0;
        }
        if (citations.length == 1) {
            return citations[0] > 0 ? 1 : 0;
        }
        
        return binarySearch(citations, 0, citations.length - 1);
    }
    
    private int binarySearch(int[] citations, int start, int end) {
        int ind = -1;
        int n = end + 1;
        while (start  <  end) {
            int mid = (start + end) / 2 + 1;
            
            if (citations[mid] > n - mid) {
                end = mid - 1;
            }
            else {
                start = mid;
            }
        }
        
        if (citations[start] > n - start) {
            return n;
        }
        
        return citations[start] == n - start ? n - start : n - start - 1;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
[1,2,100]

Output

x
+
cmd
2

#4 Code Example with Javascript Programming

Code - Javascript Programming


const hIndex = function(citations) {
  let left = 0,
    len = citations.length,
    right = len - 1,
    mid
  while (left <= right) {
    mid = left + ((right - left) >> 1)
    if (citations[mid] >= len - mid) right = mid - 1
    else left = mid + 1
  }
  return len - left
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
[1,2,100]

Output

x
+
cmd
2

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def hIndex(self, citations):
        l, r, res = 0, len(citations) - 1, 0
        while l <= r:
            mid = (l + r) // 2
            if len(citations) - mid <= citations[mid]: res, r = len(citations) - mid, r - 1
            else: l = mid + 1
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
[0,1,3,5,6]

Output

x
+
cmd
3

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0275_HIndexII
    {
        public int HIndex(int[] citations)
        {
            int n = citations.Length;
            int left = 0, right = n - 1;

            while (left  < = right)
            {
                var mid = left + (right - left) / 2;
                if (citations[mid] == n - mid) return n - mid;
                else if (citations[mid]  <  n - mid) left = mid + 1;
                else right = mid - 1;
            }

            return n - left;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
[0,1,3,5,6]

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#274 Leetcode H-Index Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#278 Leetcode First Bad Version Solution in C, C++, Java, JavaScript, Python, C# Leetcode