0%

leetcode 274 H-Index || 275 H-Index II

leetcode 274 H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 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, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

Hint:

  1. An easy approach is to sort the array first.
  2. What are the possible values of h-index?
  3. A faster approach is to use extra space.

题目地址: leetcode H-Index

题意:

给定一个数组代表论文被引用次数。要求计算h-index。

h-index定义如下:

一个学者发表的n篇论文中,他有h篇被引用至少h次,而其他的n-h篇被引用次数都不超过h。

思路:

显然,答案的范围为[0,n],n为数组的长度。

方法一

从高到底排序,然后对排完序的数组扫描。

对于i,h-index为min(i,citations[i])

C++

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(),greater<int>());
for (int i = 0; i < citations.size(); i++)
if (i >= citations[i])
return i;
return citations.size();
}
};

Python

1
2
3
4
5
6
7
8
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations: return 0
return max([min(i + 1, c) for i, c in enumerate(sorted(citations, reverse=True))])

也可以

1
2
3
4
5
6
7
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
return sum([i < c for i, c in enumerate(sorted(citations, reverse=True))])

 

如果不倒着排,则可以这么写:

n - i 是大于引用数citations[i]的个数。

对于i,h-index 为min(n - i,citations[i])

C++

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int n = citations.size();
for (int i = 0; i < n; i++)
if (citations[i] >= n - i)
return n - i;
return 0;
}
};

Python

1
2
3
4
5
6
7
8
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations: return 0
return max([min(len(citations) - i, c) for i, c in enumerate(sorted(citations))])

 

 

方法二

对引用数进行计数。这样就不需要排序啦~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations: return 0
n = len(citations)
cnt = [0] * (n + 1)
for c in citations:
if c < n:
cnt[c] += 1
else:
cnt[n] += 1
t = 0
for i in xrange(n, -1, -1):
t += cnt[i]
if t >= i:
return i
return 0

 


leetcode 275 H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Hint:

  • Expected runtime complexity is in O(log n) and the input is sorted.

题目地址: leetcode H-Index II

题意:

就是要求时间复杂度为O(log n) 的H-Index,数组已经升序排好

思路:

二分,枚举答案[0,n]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
L, R, n = 0, len(citations), len(citations)

while L < R:
mid = (L + R) >> 1
if n - mid <= citations[mid]:
R = mid
else:
L = mid + 1

return n - L
请我喝杯咖啡吧~