š 275. H ęę° II
2022幓10ę10ę„
- algorithm
š 275. H ęę° II
é¾åŗ¦: š
é®é¢ęčæ°
č§£ę³
class Solution {
public int hIndex(int[] citations) {
// ęč·Æļ¼
// äŗåļ¼ę¾å°ę大ē hļ¼ä½æå¾ [len - h] >= h
// å³ļ¼ę¾å°ęå°ē iļ¼ä½æå¾ [i] >= len - i, h = len - i
int len = citations.length;
if(citations[len - 1] == 0) {
return 0;
}
int i = mySol(citations, 0, len);
return len - i;
}
private int mySol(int[] array, int left, int right) {
// éå½ē»ę¢ę”件
int len = array.length;
if(left >= right) {
if(array[left] >= len - left) {
return left;
} else {
return left + 1;
}
}
if(left == right - 1) {
if(array[left] >= len - left) {
return left;
} else if(array[right] >= len - right){
return right;
} else {
return right + 1;
}
}
int mid = left + ((right - left) >> 1);
if(array[mid] >= len - mid) {
return mySol(array, left, mid);
} else {
return mySol(array, mid + 1, right);
}
}
}