LeetCode 274: H-Index Solution
Master LeetCode problem 274 (H-Index), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
274. H-Index
Problem Explanation
Explanation
To solve this problem, we can sort the citations array in non-decreasing order. Then, we iterate through the sorted array and for each paper with citations >= index, we increment a counter. The h-index is the maximum value of h such that the researcher has published at least h papers that have each been cited at least h times.
- Sort the citations array in non-decreasing order.
- Initialize an h-index counter to 0.
- Iterate through the sorted array and for each paper with citations >= index, increment the counter.
- Return the final h-index.
Time complexity: O(n log n) to sort the array + O(n) to iterate through the sorted array = O(n log n) Space complexity: O(1)
Solution Code
import java.util.Arrays;
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int hIndex = 0;
for (int i = 0; i < citations.length; i++) {
if (citations[i] >= citations.length - i) {
hIndex = Math.max(hIndex, citations.length - i);
}
}
return hIndex;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 274 (H-Index)?
This page provides optimized solutions for LeetCode problem 274 (H-Index) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 274 (H-Index)?
The time complexity for LeetCode 274 (H-Index) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 274 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 274 in Java, C++, or Python.