275.H指数II
链接:275.H指数II
难度:Medium
标签:数组、二分查找
简介:给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照 升序排列 。编写一个方法,计算出研究者的 h 指数。
题解 1 - python
- 编辑时间:2023-10-30
- 执行用时:40ms
- 内存消耗:21.5MB
- 编程语言:python
- 解法介绍:二分搜索。
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
题解 2 - typescript
- 编辑时间:2021-07-12
- 执行用时:80ms
- 内存消耗:42.7MB
- 编程语言:typescript
- 解法介绍:二分查找。
function hIndex(citations: number[]): number {
  const len = citations.length;
  let left = 0;
  let right = len - 1;
  while (left <= right) {
    const mid = (left + right) >> 1;
    if (citations[mid] >= len - mid) right = mid - 1;
    else left = mid + 1;
  }
  return len - left;
}
题解 3 - typescript
- 编辑时间:2021-07-12
- 执行用时:68ms
- 内存消耗:42.7MB
- 编程语言:typescript
- 解法介绍:直接求出最大值。
function hIndex(citations: number[]): number {
  if (citations.every(v => v === 0)) return 0;
  const len = citations.length;
  const max = citations[len - 1];
  const arr = new Array(max + 1).fill(0);
  citations.forEach(num => arr[num]++);
  let sum = 0;
  let ans = 0;
  for (let num = max; num >= 0; num--) {
    const count = arr[num];
    if (count === 0) continue;
    ans = Math.max(ans, Math.min((sum += count), num));
  }
  return ans;
}