Skip to content

Commit

Permalink
Implement FindHIndex.java
Browse files Browse the repository at this point in the history
  • Loading branch information
huangsam committed Nov 12, 2024
1 parent 953c8fa commit a732189
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions java/FindHIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class FindHIndex {
public int hIndex(int[] citations) {
// Values of h-index are [0, citations.length]
int[] frequency = new int[citations.length + 1];
for (int paper : citations) {
// We do not need precise frequencies after the last paper
int index = Math.min(citations.length, paper);
frequency[index]++;
}

// Start from right to left for finding maximum index faster
int sum = 0;
for (int i = frequency.length - 1; i > 0; i--) {
sum += frequency[i];
if (i <= sum) {
return i;
}
}
return 0;
}
}

0 comments on commit a732189

Please sign in to comment.