-
Notifications
You must be signed in to change notification settings - Fork 0
/
HIndex.java
56 lines (38 loc) · 1.02 KB
/
HIndex.java
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
public class HIndex {
/**
* Sorting approach
*/
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int n = citations.length;
for (int i = 0; i < n; i++) {
if (citations[i] >= n - i) {
return (n - i);
}
}
return 0;
}
}
/**
* Bucket approach
*/
class Solution {
public int hIndex(int[] citations) {
int[] bucket = new int[citations.length + 1];
for (int i = 0; i < citations.length; i++) {
if (citations[i] > citations.length) {
bucket[citations.length]++;
} else {
bucket[citations[i]]++;
}
}
int sum = 0;
for (int i = bucket.length - 1; i >= 0; i--) {
sum += bucket[i];
if (sum >= i) return i;
}
return 0;
}
}
}