forked from interviewdiscussion/files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH-Index.java
20 lines (20 loc) · 834 Bytes
/
H-Index.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) return 0;
int l = citations.length;
int[] count = new int[l + 1];//count[i] is the num of papers that has i citations (note:count[l] means >= l citations)
for (int i : citations) {
if (i > l) {
count[l]++;
} else {
count[i]++;
}
}
int res = 0;//use res to record the num of papers that has at least i citations, if res>=i, it means i is the hIndex
for (int i = l; i >= 0; i--) {
res += count[i];
if (res >= i) return i;//no need to check if other l−i papers have no more than i citations each,cuz they must be
}//lower than or equal to i
return 0;
}
}