## Algorithm

Problem Name: 274. H-Index

Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return compute the researcher's `h`-index.

According to the definition of h-index on Wikipedia: A scientist has an index `h` if `h` of their `n` papers have at least `h` citations each, and the other `n − h` papers have no more than `h` citations each.

If there are several possible values for `h`, the maximum one is taken as the `h`-index.

Example 1:

```Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.
```

Example 2:

```Input: citations = [1,3,1]
Output: 1
```

Constraints:

• `n == citations.length`
• `1 <= n <= 5000`
• `0 <= citations[i] <= 1000`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
int hIndex(int* citations, int citationsSize) {
int i, j, k = 0;
int n;
#if 0   // O(n^2) 39ms
for (i = citationsSize; i > 0; i --) { // for every possible h
n = 0;
for (j = 0; j  <  citationsSize; j ++) { // for every citation
if (citations[j] >= i) n ++;
}
if (n >= i) {
return i;
}
}
#else   // O(n) 3ms
int *x;
x = calloc(citationsSize + 1, sizeof(int));
//assert(x);
for (i = 0; i  <  citationsSize; i ++) { // for every citation
j = citations[i] > citationsSize ? citationsSize : citations[i];
x[j] ++;
}
n = 0;
for (i = citationsSize; i > 0; i --) {
n += x[i];
if (n >= i) { k = i; break; }
}
free(x);
#endif
return k;
}
``````
Copy The Code &

Input

cmd
[3,0,6,1,5]

Output

cmd
3

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int i = 0, j = citations.size() - 1;
while(j >= 0 && citations[j] > i) i++, j--;
return i;
}
};
``````
Copy The Code &

Input

cmd
[3,0,6,1,5]

Output

cmd
3

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
Arrays.sort(citations);
for (int i = n - 1; i >= 0; i--) {
if (n - i > citations[i]) {
return n - i - 1;
}
}
return n;
}
}
``````
Copy The Code &

Input

cmd
[1,3,1]

Output

cmd
1

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const hIndex = function(citations) {
const n = citations.length
const arr = Array(n + 1).fill(0)
for(let e of citations) {
if(e >= n) arr[n]++
else arr[e]++
}
for(let i = n, sum = 0; i >= 0; i--) {
sum += arr[i]
if(sum >= i) return i
}
return 0
};
``````
Copy The Code &

Input

cmd
[1,3,1]

Output

cmd
1

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def hIndex(self, citations):
citations.sort()
for i in range(len(citations)):
if len(citations) - i <= citations[i]: return len(citations) - i
return 0
``````
Copy The Code &

Input

cmd
[3,0,6,1,5]

Output

cmd
3

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

namespace LeetCode
{
public class _0274_HIndex
{
public int HIndex(int[] citations)
{
int n = citations.Length;

int[] papers = new int[n + 1];
foreach (int c in citations)
papers[Math.Min(n, c)]++;

int k = n;
for (int s = papers[n]; k > s; s += papers[k])
k--;
return k;
}
}
}
``````
Copy The Code &

Input

cmd
[3,0,6,1,5]

Output

cmd
3