g0201_0300.s0275_h_index_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0275_h_index_ii;
// #Medium #Array #Binary_Search
public class Solution {
public int hindex(int[] citations) {
int lo = 1;
int hi = 1000;
int ans = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int p = check(mid, citations);
if (citations.length - p >= mid) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private int check(int v, int[] arr) {
int lo = 0;
int hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] < v) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return lo;
}
}