Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Last active December 5, 2019 06:47
Show Gist options
  • Save shixiaoyu/323da10d46b379bee29e32d58a06fd67 to your computer and use it in GitHub Desktop.
Save shixiaoyu/323da10d46b379bee29e32d58a06fd67 to your computer and use it in GitHub Desktop.
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
// because we need from 0 to citations.length, h-index in [0, citations_length]
int[] counts = new int[citations.length + 1];
for (int i = 0; i < citations.length; i++) {
/*
if (citations[i] > citations.length) {
counts[citations.length] += 1;
} else {
counts[citations[i]] += Math.min(1, citations[i]); // this is for 0 citation case
}
this 4 lines translate into below line, sounds niubility!
*/
counts[Math.min(citations[i], citations.length)]++;
}
int citationCount = 0;
// Utilities.printIntArray(counts);
for (int i = counts.length - 1; i >= 0; i--) {
citationCount += counts[i];
if (i <= citationCount) {
return i;
}
}
// there is one and only one h-index, so this would never happen
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment