-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
HIndex.java
39 lines (35 loc) · 875 Bytes
/
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
package problems.medium;
import java.util.Arrays;
/**
* Created by sherxon on 2016-12-31.
*/
public class HIndex {
// O(NlgN) solution
public int hIndex(int[] a) {
if(a==null || a.length==0)return 0;
if(a.length==1 && a[0]==0)return 0;
Arrays.sort(a);
int max=0;
for(int i=a.length-1; i>-1; i--){
if(a[i]>max)max++;
}
return max;
}
//O(N) solution
public int hIndex2(int[] a){
int max=0;
int b[] = new int[a.length+1];
for (int i = 0; i < a.length; i++) {
if(a[i]>=b.length)b[b.length-1]++;
else b[a[i]]++;
}
for (int i = b.length-2; i >=0; i--)
b[i]+=b[i+1];
for (int i = b.length-1; i >=0; i--){
if(b[i]>=i){
return i;
}
}
return max;
}
}