LeetCode Solutions

275. H-Index II

Time: $O(\log n)$

Space: $O(1)$

			

class Solution {
 public:
  int hIndex(vector<int>& citations) {
    int l = 0;
    int r = citations.size();

    while (l < r) {
      const int m = (l + r) / 2;
      if (citations[m] >= citations.size() - m)
        r = m;
      else
        l = m + 1;
    }

    return citations.size() - l;
  }
};
			

class Solution {
  public int hIndex(int[] citations) {
    int l = 0;
    int r = citations.length;

    while (l < r) {
      final int m = (l + r) / 2;
      if (citations[m] >= citations.length - m)
        r = m;
      else
        l = m + 1;
    }

    return citations.length - l;
  }
}
			

class Solution:
  def hIndex(self, citations: List[int]) -> int:
    l = 0
    r = len(citations)

    while l < r:
      m = (l + r) // 2
      if citations[m] >= len(citations) - m:
        r = m
      else:
        l = m + 1

    return len(citations) - l