Sequentially Ordinal Rank Tracker — LeetCode 2102 Python Solution
- Problem
- #2102
- Pattern
- Heap / Priority Queue
- Reading time
- 4 min
- Source
- leetcode.com
The problem
A scenic location is represented by its name and attractiveness score, where name is a unique string among all locations and score is an integer. Locations can be ranked from the best to the worst.
Example
- Input
- ["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"]
- Output
- [null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]
- Explanation
- SORTracker tracker = new SORTracker(); // Initialize the tracker system.
Python solution
class SORTracker:
def __init__(self):
self.sl = SortedList()
self.i = -1
def add(self, name: str, score: int) -> None:
self.sl.add((-score, name))
def get(self) -> str:
self.i += 1
return self.sl[self.i][1]
# Your SORTracker object will be instantiated and called as such:
# obj = SORTracker()
# obj.add(name,score)
# param_2 = obj.get()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) |
| Space | O(n) auxiliary |
Pattern: Heap / Priority Queue
Keep only the best k elements, or always pull the smallest, in log time. LeetCode 2102. Sequentially Ordinal Rank Tracker is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Heap (Priority Queue).
The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2102. Sequentially Ordinal Rank Tracker?
- LeetCode 2102. Sequentially Ordinal Rank Tracker is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2102. Sequentially Ordinal Rank Tracker?
- The Python solution on this page runs in O(n log n).
- What is the space complexity of LeetCode 2102. Sequentially Ordinal Rank Tracker?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 2102. Sequentially Ordinal Rank Tracker cover?
- LeetCode 2102. Sequentially Ordinal Rank Tracker is tagged Design, Data Stream, Ordered Set and Heap (Priority Queue) on LeetCode.