-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path274.java
57 lines (57 loc) · 1.67 KB
/
274.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
57
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int hIndex(int[] citations) {
int check[] = new int[citations.length + 1];
for (int i = 0; i < citations.length; i++){
if (citations[i] < citations.length){
check[citations[i]] += 1;
} else {
check[check.length - 1] += 1;
}
}
int sum = 0;
for (int j = check.length - 1; j > 0; j--){
sum = sum + check[j];
if (sum >= j){
return j;
}
}
return 0;
}
}
__________________________________________________________________________________________________
sample 34592 kb submission
class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
int min = 0;
int max = citations.length;
int mid;
while (min + 1 < max) {
mid = min + (max - min) / 2;
if (findRes(citations, mid) < mid) {
max = mid;
} else {
min = mid;
}
}
if (findRes(citations, max) >= max) {
return max;
} else {
return min;
}
}
private int findRes(int[] citations, int mid) {
int cnt = 0;
for (int i = 0; i < citations.length; i++) {
if (citations[i] >= mid) {
cnt++;
}
}
return cnt;
}
}
__________________________________________________________________________________________________