274.H指数
链接:274.H指数
难度:Medium
标签:数组、计数排序、排序
简介:给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。
题解 1 - typescript
- 编辑时间:2021-07-11
- 执行用时:64ms
- 内存消耗:40.5MB
- 编程语言:typescript
- 解法介绍:计数排序,储存每个数出现的次数。
function hIndex(citations: number[]): number {
  if (citations.every(v => v === 0)) return 0;
  const max = Math.max(...citations);
  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;
}
题解 2 - python
- 编辑时间:2023-10-29
- 执行用时:36ms
- 内存消耗:16.2MB
- 编程语言:python
- 解法介绍:遍历。
class Solution:
    def hIndex(self, citations: List[int]) -> int:
        n = len(citations)
        citations.sort()
        res = 0
        for i in range(n):
            if n - i <= citations[i]: res = max(res, n - i)
        return res