15% off all plans & roadmaps Use code
See plansLeetCode 506 · Solution & editorial
You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.
Relative Ranks is a Heap (Priority Queue) Patterns problem, in the Top K Elements (Selection/Frequency) subpattern. The pattern covers top K elements, K-way merge, two heaps for median, and scheduling
Find top K elements using heap. Solve kth largest, top K frequent, and k closest points problems.
The reference solution runs in O(n log n) time and O(n) space.
Relative Ranks has been reported in interviews at Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Relative Ranks is a Heap (Priority Queue) Patterns problem, in the Top K Elements (Selection/Frequency) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.