Skip to content

275. H-Index II 👎

  • Time: $O(\log n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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