-
Notifications
You must be signed in to change notification settings - Fork 130
/
H-Index.js
73 lines (59 loc) · 3 KB
/
H-Index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
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:
An easy approach is to sort the array first.
What are the possible values of h-index?
A faster approach is to use extra space.
*/
/**
* @param {number[]} citations
* @return {number}
*/
var hIndex = function(citations) {
var len = citations.length,
i;
citations.sort(function (a, b) {
if (a < b) {
return 1;
}
return -1;
});
for (i = 0; i < len; i++) {
if (citations[i] <= i) {
return i;
}
}
return len;
};
/**
SOLUTION 2:
复杂度
时间 O(N) 空间 O(N)
思路
也可以不对数组排序,我们额外使用一个大小为N+1的数组stats。stats[i]表示有多少文章被引用了i次,这里如果一篇文章引用大于N次,我们就将其当为N次,因为H指数不会超过文章的总数。为了构建这个数组,我们需要先将整个文献引用数组遍历一遍,对相应的格子加一。统计完后,我们从N向1开始遍历这个统计数组。如果遍历到某一个引用次数时,大于或等于该引用次数的文章数量,大于引用次数本身时,我们可以认为这是H指数。之所以不用再向下找,因为我们要取最大的H指数。那如何求大于或等于某个引用次数的文章数量呢?我们可以用一个变量,从高引用次的文章数累加下来。因为我们知道,如果有x篇文章的引用大于等于3次,那引用大于等于2次的文章数量一定是x加上引用次数等于2次的文章数量。
代码
public class Solution {
public int hIndex(int[] citations) {
int[] stats = new int[citations.length + 1];
int n = citations.length;
// 统计各个引用次数对应多少篇文章
for(int i = 0; i < n; i++){
stats[citations[i] <= n ? citations[i] : n] += 1;
}
int sum = 0;
// 找出最大的H指数
for(int i = n; i > 0; i--){
// 引用大于等于i次的文章数量,等于引用大于等于i+1次的文章数量,加上引用等于i次的文章数量
sum += stats[i];
// 如果引用大于等于i次的文章数量,大于引用次数i,说明是H指数
if(sum >= i){
return i;
}
}
return 0;
}
}
*/