Hi👋SpeedAlgo • Deliberate Practice & Cognitive Ergonomics for Software Engineers

An interactive algorithm mastery and technical interview preparation platform published by Hi👋WebEnterprise. Built for senior and staff software engineers preparing for rigorous coding screens at top tech companies (FAANG/MAMAA).

12 Core Algorithmic Patterns & 196 Practice Problems

  • 1. Two Pointers (11 Paradigms, 35 Problems): Opposite ends, sorted pair sums, palindrome verification, trapping rain water, 3Sum, container with most water — includes the Sliding Window and Fast & Slow Pointers paradigms (contiguous subarray invariants, longest substrings, Floyd's cycle detection, linked list middle).
  • 2. Binary Search (9 Paradigms, 14 Problems): Monotonic predicate partitioning (`feasible(x)`), boundary search, rotated sorted arrays, square roots, minimum ship capacity, median of two sorted arrays, matrix median on value range.
  • 3. Bit Manipulation (5 Paradigms, 8 Problems): Counting set bits, XOR cancellation, bit reversal, bitmask pairing, maximum XOR with a bitwise trie.
  • 4. Math & Geometry (8 Paradigms, 14 Problems): Sieve of Eratosthenes, integer/roman conversions, modular arithmetic, geometric simulation.
  • 5. Tree/Graph Depth-First Search (10 Paradigms, 19 Problems): Path sums, lowest common ancestor, tree diameter, validating BSTs, tree DP (House Robber III, Binary Tree Cameras).
  • 6. Tree/Graph Breadth-First Search (4 Paradigms, 11 Problems): Level-order traversals, shortest path in unweighted graphs, zig-zag traversals, rotting oranges, word ladders.
  • 7. Graphs (10 Paradigms, 18 Problems): Topological sort, cycle detection, Dijkstra's algorithm, bipartite graph validation, network delay.
  • 8. Backtracking (6 Paradigms, 14 Problems): Subsets, permutations, combinations, constraint satisfaction, pruning, N-Queens, Sudoku solver.
  • 9. Dynamic Programming (11 Paradigms, 18 Problems): Memoization vs tabulation, 0/1 knapsack, unbounded knapsack, coin change, edit distance, longest common subsequence.
  • 10. Heap / Priority Queue (9 Paradigms, 11 Problems): Running medians, top-k elements, interval scheduling, IPO, k-way merges.
  • 11. Advanced Data Structures (6 Paradigms, 15 Problems): Trie (prefix tree), Union-Find (Disjoint Set Union), LRU Cache, LFU Cache, range-sum queries with updates.
  • 12. Intervals & Stack / Miscellaneous (12 Paradigms, 19 Problems): Merge intervals, daily temperatures, largest rectangle in histogram, trapping rain water via stack.

4-Stage Deliberate Practice Framework

  1. Stage 1 (Compare & Learn): Read the pattern's mental model, loop invariant and Big-O proof, compare the pattern's template with the problem's solution side by side, and use the step-by-step debugger. The solution can also be shown in C#, Java, TypeScript, C++, Go or Rust for almost every problem.
  2. Stage 2 (Active Recall): Rebuild the solution from memory in a code editor, with an invariant checklist and optional progressive hints, and check it against a judged test suite.
  3. Stage 3 (AI Interview Coach, coming soon): A mock-interview stage still in development; for now, students go straight from Stage 2 to Stage 4.
  4. Stage 4 (Solve on Your Own): Solve a new problem on your own against a 15-minute timer; your code is graded against a test suite. Python and TypeScript run in your browser and C# runs on our server (design problems grade in Python and TypeScript).

Equipped with a spaced-repetition review hub on a fixed 7-step schedule (1 day up to 90 days), a two-pane Studio workspace, and study notes.

Pricing, Access & Commercial Terms

  • Core Curriculum: 100% Free. No credit card required.
  • Coins: New accounts get 40 free coins once, the daily claim adds +20 coins once a day, and inviting a friend earns you +25 coins when they complete their first practice step. These are the only ways to earn coins.
  • AI Coach: Each AI coach question costs 1-5 coins, priced by the server according to its size.
  • BYOK (Bring Your Own Key): AI coaching is free with your own Google Gemini, OpenAI or OpenRouter API key (BYOK).
  • Extras: Coins also unlock extras for 24 hours: a Recall hint level (1 coin), a Solve solution reveal (2), a LeetCode problem card's solutions (1) and a pattern's extra canonical problems (1).
  • Payments & Refunds: Nothing on the site is sold for money: coins can only be earned, never bought. There are no subscriptions or charges, so there is nothing to refund.
  • Platform Operator: Hi👋WebEnterprise. Support & policies at hispeedalgo.com.
Skip to main content
Hi👋SpeedAlgo

Invariant-First Algorithmic Mastery

208Items
Theory Context•Miscellaneous & Sweeps
MediumLC 274

H-Index (LeetCode 274)

A researcher has published n papers, and citations[i] is the number of times paper i has been cited. Their h-index is the largest number h for which at least h of the papers each have h or more citations.

Return the researcher's h-index.

Worked Examples

Example 1
Input:citations = [3,0,6,1,5]
Output:3
3001621354>= 3>= 3>= 3
Explanation: Five papers. Three of them (with 6, 5 and 3 citations) have at least 3 each, so `h = 3` works. `h = 4` would need four papers with 4 or more citations, and only two (6 and 5) have that.
Example 2
Input:citations = [1,3,1]
Output:1
103112only one >= 2
Explanation: Every paper has at least 1 citation, so `h = 1` works. `h = 2` would need two papers with 2 or more citations, and only the paper with 3 has that.

⚖️Formal Constraints & Bounds

  • n == citations.length

  • 1 <= n <= 5000

  • 0 <= citations[i] <= 1000

Deep-Dive & Conceptual Insights

Core Insight

The h-index can never be larger than n, the number of papers. So a paper with more than n citations counts the same as a paper with n. The code uses n + 1 slots and fills them in one loop, with no comparisons. It then walks from h = n down and adds up the slots. At each h, the running total is the number of papers with at least h citations.

Real-World Scenario & Production Applications

Some questions ask for a threshold that depends on a count. One example is an author's h-index in a citation database. Another is the largest k such that k servers each have k or more free slots. Counting the items per value, up to the number of items, answers each question in two passes. No sort is needed.

Step-by-Step Execution Trace Table

The debugger's first preset is Example 1, citations = [3,0,6,1,5]. Here n = 5, so count has slots 0 to 5. The counting loop puts 3, 0, 1 and 5 into the slots with the same numbers. It puts 6 into slot min(6, 5) = 5. After the loop, count = [1, 1, 0, 1, 0, 2]. The walk then goes as follows:

hcount[h] addedpaperspapers < h?
5count[5] = 2 (the start)22 < 5 is true, so lower h
4count[4] = 022 < 4 is true, so lower h
3count[3] = 133 < 3 is false, so return 3
Scroll horizontally to see all columns, or expand to full screen
Code Shape
Archetype Code Shape
Python
1Don't sort. The h-index is at most `n`, so only the citation values from 0 to `n` matter. Give each value its own slot and count the papers in each slot. Put every count of `n` or more into slot `n`.
2While `h` goes down from `n`, keep `papers` equal to the number of papers with at least `h` citations. The first `h` with `papers >= h` is the answer.
3The shape of the code: make a list of `n + 1` zeros. In one `for` loop, add 1 to each paper's slot. Then set `h = n` and `papers = count[n]`. In a `while papers < h` loop, lower `h` by 1 and add `count[h]`. Return `h`.
4The trap is the slot index. Write `count[min(c, n)] += 1`, so every paper with `n` or more citations goes into slot `n`. On `[100]`, `n = 1` and the list has 2 slots. Without `min`, `count[100] += 1` raises an IndexError.

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Sorting solves this problem. Sort the citations from most cited to least cited. The h-index is then the last position h whose paper still has h or more citations. A comparison sort takes O(N log N) time. But the values here are whole numbers. Also, only the values from 0 to n matter, because the h-index can never be larger than the number of papers. Counting Sort uses these two facts. Make one slot for each value: count = [0] * (n + 1). Add each paper to the slot of its citation count with count[min(c, n)] += 1. No two papers are compared. The slots are already in order of value. So walk them from h = n down to 0 and add them up. At each h, the total is the number of papers with at least h citations.

🗂️ The Analogy: Exam Papers in Mark Trays

A teacher has 30 exam papers with marks from 0 to 100. The teacher wants them in order of mark. The teacher does not compare papers two at a time. Instead, the teacher sets out one tray for each mark and puts each paper into the tray for its mark. Then the teacher picks up the trays from 100 down to 0, and the papers come out in order. For the h-index, the trays only go up to n. A paper with more citations than there are papers goes into the top tray. Any count of n or more tells us the same thing about h.

🔑 Why It Works

After the counting loop, count[v] is the number of papers with exactly v citations, for every v below n. count[n] is the number of papers with n or more citations. During the walk, papers adds count[h] each time h goes down by 1. So papers is always the number of papers with at least h citations. As h goes down, papers can only go up. So the first h with papers >= h is the largest h that works. The loop always ends. At h = 0, the test papers >= 0 is always true.

  • Indexing count[c] without the cap at n

  • Stopping only when papers == h

  • Adding count[h] before lowering h

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

The statement asks for "the largest h such that at least h papers have h or more citations". Every citation is between 0 and 1000, and there are at most 5000 papers. The answer comes from the papers in sorted order, and the values are small whole numbers. Small whole-number values in a known range are the signal for Counting Sort. Here the range can be cut to 0 to n, because the answer can never be larger than the number of papers.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

While h goes down from n, papers is the number of papers with at least h citations. It starts as papers = count[n]. After each h -= 1, the code runs papers += count[h]. Stop at the first h with papers >= h. That h is the largest one that works.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Write count[min(c, n)] += 1, so every paper with n or more citations goes into slot n. On [100], n = 1 and the list has 2 slots. Without min, count[100] += 1 raises an IndexError.

  • Keep the condition while papers < h. Do not test papers == h. On [1,1,1], papers jumps from 0 to 3 when h reaches 1, so an == test never stops at the answer.

  • Lower h first, then add count[h]. If you add first, the loop adds count[n] twice. Then [3,0,6,1,5] returns 4 instead of 3.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I'd use Counting Sort. The h-index can never be larger than the number of papers, n. So only citation values from 0 to n matter, and a count above n works exactly like n. I make n plus 1 slots. I add each paper to the slot of its citation count, capped at n. No two papers are ever compared. Then I walk h down from n. I keep a running total of the papers with at least h citations. Each time I lower h, I add the papers with exactly h citations. The first h where the total reaches h is the largest h that works. At h equal to 0 the test always passes, so the loop ends. The trap is the cap. Citations go up to 1000 while n can be 1, so without the cap the index goes past the end of the list. The time is O(n) and the space is O(n).

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the code. count = [0] * (n + 1) writes n + 1 zeros, which is O(N). The for c in citations loop does one min and one addition per paper, which is O(N). The while papers < h loop lowers h by 1 each time. It stops at h = 0 at the latest, because papers is never negative. So it runs at most n times, with O(1) work each time, which is O(N). The total is O(N). The size of the citation values does not change the cost. Because of the cap, the list has n + 1 slots whether a paper has 5 citations or 1000.

SPACE COMPLEXITY

O(N)

count holds n + 1 integers, which is O(N). h and papers are two numbers, and the answer is one integer.

Formal Recurrence Relation

T(N) = O(N + 1) + N · O(1) + at most N · O(1) = O(N)

Derivation Progression

Slots

O(N + 1)

count = [0] * (n + 1) writes one zero per possible answer.

Counting pass

N · O(1)

The for c in citations loop does one min(c, n) and one += 1 for each paper.

Walk down

at most N · O(1)

Each step of while papers < h does h -= 1 and one addition. h never goes below 0.

Total

O(N)

One pass over the papers and at most one pass over the slots.

Variable Definitions

NNN

Number of papers, len(citations) (called n in the code)

hhh

The candidate h-index, walking down from n

paperspaperspapers

Number of papers with at least h citations

Memory Architecture & Bounds

🟣 Call Stack

O(1): iterative, no recursion

🔵 Auxiliary Heap

O(N): count, n + 1 integers

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N). The counting loop reads every paper, even when the walk stops at once, as in [5,5,5,5,5]

Average Case

O(N)O(N)O(N)

Worst Case

O(N)O(N)O(N). Every paper is counted, and h walks from n down to 0, as in [0,0,0]

Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "at least h papers", "h or more citations", "0 <= citations[i] <= 1000". The answer comes from the values in sorted order, and the values are small whole numbers. This points to Counting Sort. The range can be cut to 0 to n, because the h-index can never be larger than n.

CONSTRAINTS & BOUNDS

At these limits, a sort would also be fast. The cap matters when the values grow. A citation database can hold papers with hundreds of thousands of citations. The slot list still has only n + 1 entries, one for each possible answer.

FAANG PRODUCTION TRAPS & EDGE CASES

The papers may be spread over several shards (servers that each hold part of the data). Each shard can build its own count over the same slots. To merge, add the counts slot by slot, then walk the merged list once. Every shard must cap at the total number of papers n, not at its own number of papers. Otherwise a paper with more citations than its shard has papers goes into the wrong slot.

Core Algorithmic State Invariants

  1. Capping Loses Nothing

[3,0,6,1,5] and [3,0,1000,1,5] build the same list, count = [1, 1, 0, 1, 0, 2]. So both answers are 3. A paper with 6 citations and a paper with 1000 are both counted in every papers total from h = 5 down. The walk cannot tell them apart.

  1. When Every Paper Has 0 Citations

On [0,0,0], count = [3, 0, 0, 0]. papers stays 0 while h goes 3, 2, 1. Only at h = 0 does papers become 3. The test 3 >= 0 ends the loop, so the answer 0 needs no special case.

Theory Context•Miscellaneous & Sweeps
MediumLC 274

H-Index (LeetCode 274)

A researcher has published n papers, and citations[i] is the number of times paper i has been cited. Their h-index is the largest number h for which at least h of the papers each have h or more citations.

Return the researcher's h-index.

Worked Examples

Example 1
Input:citations = [3,0,6,1,5]
Output:3
3001621354>= 3>= 3>= 3
Explanation: Five papers. Three of them (with 6, 5 and 3 citations) have at least 3 each, so `h = 3` works. `h = 4` would need four papers with 4 or more citations, and only two (6 and 5) have that.
Example 2
Input:citations = [1,3,1]
Output:1
103112only one >= 2
Explanation: Every paper has at least 1 citation, so `h = 1` works. `h = 2` would need two papers with 2 or more citations, and only the paper with 3 has that.

⚖️Formal Constraints & Bounds

  • n == citations.length

  • 1 <= n <= 5000

  • 0 <= citations[i] <= 1000

Deep-Dive & Conceptual Insights

Core Insight

The h-index can never be larger than n, the number of papers. So a paper with more than n citations counts the same as a paper with n. The code uses n + 1 slots and fills them in one loop, with no comparisons. It then walks from h = n down and adds up the slots. At each h, the running total is the number of papers with at least h citations.

Real-World Scenario & Production Applications

Some questions ask for a threshold that depends on a count. One example is an author's h-index in a citation database. Another is the largest k such that k servers each have k or more free slots. Counting the items per value, up to the number of items, answers each question in two passes. No sort is needed.

Step-by-Step Execution Trace Table

The debugger's first preset is Example 1, citations = [3,0,6,1,5]. Here n = 5, so count has slots 0 to 5. The counting loop puts 3, 0, 1 and 5 into the slots with the same numbers. It puts 6 into slot min(6, 5) = 5. After the loop, count = [1, 1, 0, 1, 0, 2]. The walk then goes as follows:

hcount[h] addedpaperspapers < h?
5count[5] = 2 (the start)22 < 5 is true, so lower h
4count[4] = 022 < 4 is true, so lower h
3count[3] = 133 < 3 is false, so return 3
Scroll horizontally to see all columns, or expand to full screen
Code Shape
Archetype Code Shape
Python
1Don't sort. The h-index is at most `n`, so only the citation values from 0 to `n` matter. Give each value its own slot and count the papers in each slot. Put every count of `n` or more into slot `n`.
2While `h` goes down from `n`, keep `papers` equal to the number of papers with at least `h` citations. The first `h` with `papers >= h` is the answer.
3The shape of the code: make a list of `n + 1` zeros. In one `for` loop, add 1 to each paper's slot. Then set `h = n` and `papers = count[n]`. In a `while papers < h` loop, lower `h` by 1 and add `count[h]`. Return `h`.
4The trap is the slot index. Write `count[min(c, n)] += 1`, so every paper with `n` or more citations goes into slot `n`. On `[100]`, `n = 1` and the list has 2 slots. Without `min`, `count[100] += 1` raises an IndexError.

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Sorting solves this problem. Sort the citations from most cited to least cited. The h-index is then the last position h whose paper still has h or more citations. A comparison sort takes O(N log N) time. But the values here are whole numbers. Also, only the values from 0 to n matter, because the h-index can never be larger than the number of papers. Counting Sort uses these two facts. Make one slot for each value: count = [0] * (n + 1). Add each paper to the slot of its citation count with count[min(c, n)] += 1. No two papers are compared. The slots are already in order of value. So walk them from h = n down to 0 and add them up. At each h, the total is the number of papers with at least h citations.

🗂️ The Analogy: Exam Papers in Mark Trays

A teacher has 30 exam papers with marks from 0 to 100. The teacher wants them in order of mark. The teacher does not compare papers two at a time. Instead, the teacher sets out one tray for each mark and puts each paper into the tray for its mark. Then the teacher picks up the trays from 100 down to 0, and the papers come out in order. For the h-index, the trays only go up to n. A paper with more citations than there are papers goes into the top tray. Any count of n or more tells us the same thing about h.

🔑 Why It Works

After the counting loop, count[v] is the number of papers with exactly v citations, for every v below n. count[n] is the number of papers with n or more citations. During the walk, papers adds count[h] each time h goes down by 1. So papers is always the number of papers with at least h citations. As h goes down, papers can only go up. So the first h with papers >= h is the largest h that works. The loop always ends. At h = 0, the test papers >= 0 is always true.

  • Indexing count[c] without the cap at n

  • Stopping only when papers == h

  • Adding count[h] before lowering h

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

The statement asks for "the largest h such that at least h papers have h or more citations". Every citation is between 0 and 1000, and there are at most 5000 papers. The answer comes from the papers in sorted order, and the values are small whole numbers. Small whole-number values in a known range are the signal for Counting Sort. Here the range can be cut to 0 to n, because the answer can never be larger than the number of papers.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

While h goes down from n, papers is the number of papers with at least h citations. It starts as papers = count[n]. After each h -= 1, the code runs papers += count[h]. Stop at the first h with papers >= h. That h is the largest one that works.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Write count[min(c, n)] += 1, so every paper with n or more citations goes into slot n. On [100], n = 1 and the list has 2 slots. Without min, count[100] += 1 raises an IndexError.

  • Keep the condition while papers < h. Do not test papers == h. On [1,1,1], papers jumps from 0 to 3 when h reaches 1, so an == test never stops at the answer.

  • Lower h first, then add count[h]. If you add first, the loop adds count[n] twice. Then [3,0,6,1,5] returns 4 instead of 3.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I'd use Counting Sort. The h-index can never be larger than the number of papers, n. So only citation values from 0 to n matter, and a count above n works exactly like n. I make n plus 1 slots. I add each paper to the slot of its citation count, capped at n. No two papers are ever compared. Then I walk h down from n. I keep a running total of the papers with at least h citations. Each time I lower h, I add the papers with exactly h citations. The first h where the total reaches h is the largest h that works. At h equal to 0 the test always passes, so the loop ends. The trap is the cap. Citations go up to 1000 while n can be 1, so without the cap the index goes past the end of the list. The time is O(n) and the space is O(n).

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the code. count = [0] * (n + 1) writes n + 1 zeros, which is O(N). The for c in citations loop does one min and one addition per paper, which is O(N). The while papers < h loop lowers h by 1 each time. It stops at h = 0 at the latest, because papers is never negative. So it runs at most n times, with O(1) work each time, which is O(N). The total is O(N). The size of the citation values does not change the cost. Because of the cap, the list has n + 1 slots whether a paper has 5 citations or 1000.

SPACE COMPLEXITY

O(N)

count holds n + 1 integers, which is O(N). h and papers are two numbers, and the answer is one integer.

Formal Recurrence Relation

T(N) = O(N + 1) + N · O(1) + at most N · O(1) = O(N)

Derivation Progression

Slots

O(N + 1)

count = [0] * (n + 1) writes one zero per possible answer.

Counting pass

N · O(1)

The for c in citations loop does one min(c, n) and one += 1 for each paper.

Walk down

at most N · O(1)

Each step of while papers < h does h -= 1 and one addition. h never goes below 0.

Total

O(N)

One pass over the papers and at most one pass over the slots.

Variable Definitions

NNN

Number of papers, len(citations) (called n in the code)

hhh

The candidate h-index, walking down from n

paperspaperspapers

Number of papers with at least h citations

Memory Architecture & Bounds

🟣 Call Stack

O(1): iterative, no recursion

🔵 Auxiliary Heap

O(N): count, n + 1 integers

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N). The counting loop reads every paper, even when the walk stops at once, as in [5,5,5,5,5]

Average Case

O(N)O(N)O(N)

Worst Case

O(N)O(N)O(N). Every paper is counted, and h walks from n down to 0, as in [0,0,0]

Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "at least h papers", "h or more citations", "0 <= citations[i] <= 1000". The answer comes from the values in sorted order, and the values are small whole numbers. This points to Counting Sort. The range can be cut to 0 to n, because the h-index can never be larger than n.

CONSTRAINTS & BOUNDS

At these limits, a sort would also be fast. The cap matters when the values grow. A citation database can hold papers with hundreds of thousands of citations. The slot list still has only n + 1 entries, one for each possible answer.

FAANG PRODUCTION TRAPS & EDGE CASES

The papers may be spread over several shards (servers that each hold part of the data). Each shard can build its own count over the same slots. To merge, add the counts slot by slot, then walk the merged list once. Every shard must cap at the total number of papers n, not at its own number of papers. Otherwise a paper with more citations than its shard has papers goes into the wrong slot.

Core Algorithmic State Invariants

  1. Capping Loses Nothing

[3,0,6,1,5] and [3,0,1000,1,5] build the same list, count = [1, 1, 0, 1, 0, 2]. So both answers are 3. A paper with 6 citations and a paper with 1000 are both counted in every papers total from h = 5 down. The walk cannot tell them apart.

  1. When Every Paper Has 0 Citations

On [0,0,0], count = [3, 0, 0, 0]. papers stays 0 while h goes 3, 2, 1. Only at h = 0 does papers become 3. The test 3 >= 0 ends the loop, so the answer 0 needs no special case.

Rosetta Dual-Monaco Comparison
Python 3
CANONICAL INVARIANT TEMPLATE
Loading...
CONCRETE: H-INDEX (LEETCODE 274)
T = O(N)S = O(N)
Loading...
Pattern Implementation Mapping TableCanonical Invariant ⟷ Concrete Code ⟷ Engineering Rationale
Canonical InvariantConcrete CodeEngineering Rationale
One slot per possible answercount = [0] * (n + 1)`h` is a whole number from 0 to `n`. So slots 0 to `n` cover every possible answer.
Count each value in its own slot, capped at n (the trap)for c in citations: count[min(c, n)] += 1No two values are compared. A count above `n` works like `n`, because `h` can never be larger than the number of papers. The cap also keeps the index inside the list.
Start at the largest possible answerh = n papers = count[n]`papers` holds the number of papers with at least `h` citations. At `h = n`, that is only the top slot.
Walk the slots down in order of valuewhile papers < h: h -= 1 papers += count[h]When `h` goes down by 1, the papers with exactly the new `h` citations are added. So `papers` stays the number of papers with at least `h` citations.
Answerreturn hThe first `h` from the top with `papers >= h` is the largest `h` that works.
© 2026 Hi👋WebEnterprise. All rights reserved.
Sitemap•llms.txt•