H-Index (with.Java)
This is an article about the H-Index (with.Java) problem.
I would like to solve the coding test problem and reflect on the problem I solved.
Letās get to the problem first.
Problem
The H-Index is an indicator of a scientistās productivity and influence.
You want to find the value h that represents the H-Index of a scientist.
According to Wikipedia 1, H-Index is obtained as follows.
Of the n papers published by a scientist, if more than h are cited and the rest are cited less than h, the maximum value of h is this scientistās H-Index.
Write a solution function to return this scientistās H-Index, given the sequence citations of the number of citations of a paper published by a scientist as a parameter.
Restrictions
- The number of papers published by scientists is more than one and less than 1,000.
- The number of citations per paper is 0 or more and 10,000 or less.
Input/Output Examples
citations | return |
---|---|
[3, 0, 6, 1, 5] | 3 |
my solution to the problem
import java.util.Arrays;
class Solution {
public int solution(int[] citations) {
int answer = 0;
Arrays.sort(citations);
for(int i = 0; i < citations.length; i++){
int temp = Math.min(citations[i], citations.length - i);
if(temp >= answer){
answer = temp;
}else{
break;
}
}
return answer;
}
}
Solution Description
- The solution method takes integer array citations as input.
- First, sort the citations arrays in ascending order, which is a common approach to sort the arrays to calculate the H-Index.
- Then use the for loop to traverse the array.
- Choose a smaller value between each citation count citations[i] and the remaining number of citations.length -i, which makes the current H-Index candidate.
- If the current H-Index candidate is greater or equal to the previous H-Index candidate, update the answer.
- The subsequent values are smaller, meaning that the H-Index is no longer increasing, so end the iteration with break.
- Finally, answer is returned.
Conclusion
The code solves the problem of computing H-Index using an array of number of thesis citations, which uses an iterative statement to find and verify H-Index candidates after arranging the arrays.