🎨Now live: Try our Free AI Image Generation Feature

Intuition
The h-index is a metric that aims to measure both the productivity and citation impact of a researcher's publications. To find the h-index, we need to determine the maximum value h
such that the researcher has at least h
papers with h
or more citations.
Approach
- Sort the citations: Start by sorting the array of citations in ascending order.
- Initialize a counter: Use a counter
h
to keep track of the h-index value. - Iterate through the sorted citations:
- For each citation
citations[i]
, check if it meets the conditioncitations[i] >= length - i
. - If the condition is met, increment the h-index counterh
. - Return the h-index: The final value of
h
after the loop will be the h-index.
Complexity
Time Complexity
- Sorting: Sorting the array takes O(n log n), where
n
is the length of the array. - Iteration: Iterating through the sorted array takes O(n).
Thus, the overall time complexity is O(n log n).
Space Complexity
- The space complexity is O(1) if we consider the sorting to be done in-place. If the sorting algorithm requires additional space, it may take O(n) space. Other than sorting, we only use a few extra variables.
Code
C++
class Solution
{
public:
int hIndex(vector& citations)
{
sort(citations.rbegin(),citations.rend());
int n=citations.size();
int i;
for(i=0;i=(i+1))
{
continue;
}
else
{
break;
}
}
return i;
}
};
Python
class Solution:
def hIndex(self, citations: List[int]) -> int:
citations.sort()
h = 0
length = len(citations)
for i in range(length):
item = citations[i]
if item >= length - i:
h += 1
return h
Java
import java.util.Scanner;
class Solution {
public int hIndex(int[] citations) {
sortArray(citations);
int n = citations.length;
for (int i = 0; i < n; i++) {
int h = n - i;
if (citations[i] >= h) {
return h;
}
}
return 0;
}
private void sortArray(int[] arr) {
int n = arr.length;
int min=0;
for(int i=0;i
JavaScript
/**
* @param {number[]} citations
* @return {number}
*/
var hIndex = function (citations) {
citations.sort((a, b) => a - b);
let h = 0;
const length = citations.length;
for (let i = 0; i < length; i++) {
const item = citations[i];
if (item >= length - i) {
console.log(i, item, length - i);
h++;
}
}
return h;
};