Skip to content

Commit f7646c4

Browse files
committed
Create HIndex.java
274
1 parent aad6249 commit f7646c4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

HIndex.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Project Name : Leetcode
7+
* Package Name : leetcode
8+
* File Name : HIndex
9+
* Creator : Leo
10+
* Description : 274. H-Index
11+
*/
12+
public class HIndex {
13+
/**
14+
* For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total
15+
* and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at
16+
* least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
17+
18+
* 0 1 2 3 4
19+
* 6 5 3 1 0
20+
*/
21+
// time : O(nlogn) space : O(1)
22+
public int hIndex(int[] citations) {
23+
Arrays.sort(citations);
24+
int res = 0;
25+
while (res < citations.length && citations[citations.length - 1 - res] > res) {
26+
res++;
27+
}
28+
return res;
29+
}
30+
31+
// time : O(n) space : O(n)
32+
public int hIndex2(int[] citations) {
33+
int[] helper = new int[citations.length + 1];
34+
for (int i = 0; i < citations.length; i++) {
35+
helper[citations[i] <= citations.length ? citations[i] : citations.length] += 1;
36+
}
37+
int sum = 0;
38+
for (int i = citations.length; i > 0; i--) {
39+
sum += helper[i];
40+
if (sum >= i) {
41+
return i;
42+
}
43+
}
44+
return 0;
45+
}
46+
}

0 commit comments

Comments
 (0)