275. H-Index II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int left = 0, right = n - 1;

int lastIndex = n;
while (left <= right) {
int mid = (left + right) >>> 1;

if (citations[mid] >= n - mid) {
lastIndex = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}

return n - lastIndex;
}
}

Binary Search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int left = 0, right = n - 1;

while (left <= right) {
int mid = (left + right) >>> 1;

if (citations[mid] >= n - mid) {
right = mid - 1;
} else {
left = mid + 1;
}
}

// now: right, left
return n - left; // left 为最后一个满足条件的索引
}
}

References

275. H-Index II