Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

operations =
arguments =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Maintain a min-heap of size k containing the k largest elements seen so far.

Approach

  • Heap holds at most k elements.
  • When adding a value, push it, then pop if size exceeds k.
  • The heap’s minimum is the k-th largest.

Why it works

The heap always stores exactly the k largest elements; the smallest among them is the k-th largest.

Code
Loading...
Complexity
Time: O(log k) per add
Space: O(k)
Solution
Updated: 2026-02-23

Idea

Maintain a min-heap of size k containing the k largest elements seen so far.

Approach

  • Heap holds at most k elements.
  • When adding a value, push it, then pop if size exceeds k.
  • The heap’s minimum is the k-th largest.

Why it works

The heap always stores exactly the k largest elements; the smallest among them is the k-th largest.

Code
Loading...
Complexity
Time: O(log k) per add
Space: O(k)