-
Notifications
You must be signed in to change notification settings - Fork 0
/
H-Index.java
54 lines (38 loc) · 1.04 KB
/
H-Index.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
import java.util.*;
class Solution {
public static int[] refCounts;
public int solution(int[] citations) {
Arrays.sort(citations);
int minv = Integer.MAX_VALUE;
int maxv = 0;
for (int refCount : citations) {
minv = Math.min(minv, refCount);
maxv = Math.max(maxv, refCount);
}
refCounts = new int[maxv + 1];
for (int refCount : citations) {
refCounts[refCount]++;
}
int answer = 0;
for (int i = maxv; i >= 0; --i) {
if (isPossible(i)) {
answer = i;
break;
}
}
return answer;
}
public boolean isPossible(int val) {
int over = 0;
int under = 0;
for (int i = val; i < refCounts.length; ++i) {
over += refCounts[i];
}
if (val != 0) {
for (int i = val - 1; i >= 0; --i) {
under += refCounts[i];
}
}
return over >= val && under < val;
}
}