public class Solution {
public int hIndex(int[] citations) {
if(citations == null  citations.length == 0){
return 0;
}
Arrays.sort(citations);
int length = citations.length;
if(citations[0] >= length) {
return length;
}
int result = 0;
for(int i = 0; i < length; i++){
if(citations[length  1  i] <= i && length  i >= 0 && length  i < length && citations[length  i] >= i){
result = Math.max(result, i);
}
}
return result;
}
}
Java solution with O(1) space complexity


If you use the sort approach, then the code can be simplified as:
public int hIndex(int[] citations) { Arrays.sort(citations); for(int i = 0; i < citations.length; i++){ if(citations[i] >= citations.length  i){ return citations.length  i; } } return 0; }
Since we are sorting the array in ascending order, the H index now become the first index that has citations more than its remaining published papers.
See pic from wiki for better illustration:
https://commons.wikimedia.org/wiki/File%3AHindexen.svg