Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
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.length1 <= n <= 50000 <= citations[i] <= 1000Problem summary: 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. According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,0,6,1,5]
[1,3,1]
h-index-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #274: H-Index
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int n = citations.length;
for (int h = n; h > 0; --h) {
if (citations[n - h] >= h) {
return h;
}
}
return 0;
}
}
// Accepted solution for LeetCode #274: H-Index
func hIndex(citations []int) int {
sort.Ints(citations)
n := len(citations)
for h := n; h > 0; h-- {
if citations[n-h] >= h {
return h
}
}
return 0
}
# Accepted solution for LeetCode #274: H-Index
class Solution:
def hIndex(self, citations: List[int]) -> int:
citations.sort(reverse=True)
for h in range(len(citations), 0, -1):
if citations[h - 1] >= h:
return h
return 0
// Accepted solution for LeetCode #274: H-Index
impl Solution {
#[allow(dead_code)]
pub fn h_index(citations: Vec<i32>) -> i32 {
let mut citations = citations;
citations.sort_by(|&lhs, &rhs| rhs.cmp(&lhs));
let n = citations.len();
for i in (1..=n).rev() {
if citations[i - 1] >= (i as i32) {
return i as i32;
}
}
0
}
}
// Accepted solution for LeetCode #274: H-Index
function hIndex(citations: number[]): number {
citations.sort((a, b) => b - a);
for (let h = citations.length; h; --h) {
if (citations[h - 1] >= h) {
return h;
}
}
return 0;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.