> For the complete documentation index, see [llms.txt](https://chunhthanhde.gitbook.io/leetcode-top-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-1-array-string/011-h-index.md).

# 274. H-Index 📊

**Difficulty**: `Medium` - **Tags**: `Array`, `Sorting`

## Description

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

The **h-index** is defined as the maximum value of `h` such that the researcher has published at least `h` papers that have each been cited at least `h` times.

## Examples

**Example 1:**

Input:

```python
citations = [3,0,6,1,5]
```

Output:

```
3
```

Explanation: The researcher has 5 papers in total, with citation counts of `[3,0,6,1,5]`.

* The h-index is 3 because there are 3 papers with at least 3 citations each, and the remaining 2 papers have fewer than 3 citations.

**Example 2:**

Input:

```python
citations = [1,3,1]
```

Output:

```
1
```

Explanation: The researcher has 3 papers with citation counts `[1,3,1]`.

* The h-index is 1 because there is 1 paper with at least 1 citation, and the remaining papers have fewer than 1 citation.

***

## Solution 💡

Sort the citations array in descending order. The h-index is the maximum value of `h` such that there are at least `h` papers with `h` or more citations.

### Java

```java
import java.util.Arrays;

class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int n = citations.length;
        for (int i = 0; i < n; i++) {
            if (citations[i] >= n - i) {
                return n - i;
            }
        }
        return 0;
    }
}
```

### Time Complexity ⏳

* Sorting the array takes `O(n log n)`, where `n` is the number of citations.
* The for loop iterates through the sorted array, taking `O(n)` time.

Thus, the overall time complexity is **O(n log n)**.

### Space Complexity 💾

* The space complexity is **O(1)** since the sorting is done in-place and we are using only a few extra variables.

You can find the full `Solution.java` file [here](https://github.com/ChunhThanhDe/Leetcode-Top-Interview/blob/main/Topic%201%20Array%20-%20String/011%20H-Index/Solution2.java) and the best Solution is [here](https://github.com/ChunhThanhDe/Leetcode-Top-Interview/blob/main/Topic%201%20Array%20-%20String/011%20H-Index/Solution.java).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-1-array-string/011-h-index.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
