Skip to content

Instantly share code, notes, and snippets.

@cc2011
Created August 18, 2017 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cc2011/4eb860359864b27f5dd86e1ce1456c53 to your computer and use it in GitHub Desktop.
Save cc2011/4eb860359864b27f5dd86e1ce1456c53 to your computer and use it in GitHub Desktop.
H index
class Solution {
/*
public int hIndex(int[] citations) {
Arrays.sort(citations);
int res = citations.length;
for(int i=0; i < citations.length; i++) {
if( res > citations[i] ) {
res--;
} else {
return res;
}
}
return 0;
}
*/
// https://discuss.leetcode.com/topic/40765/java-bucket-sort-o-n-solution-with-detail-explanation
public int hIndex(int[] citations) {
int n = citations.length;
int[] res = new int[n+1];
for(int i=0; i < citations.length; i++) {
if( citations[i] >= n) {
res[n]++;
} else {
res[citations[i]]++;
}
}
int count =0;
for(int i=n; i >=0; i--) {
count += res[i];
if (count >= i) {
return i;
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment