Sequentially Ordinal Rank Tracker Problem
Description
LeetCode Problem 2102.
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. The higher the score, the better the location. If the scores of two locations are equal, then the location with the lexicographically smaller name is better.
You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports:
- Adding scenic locations, one at a time.
- Querying the
i^thbest location of all locations already added, whereiis the number of times the system has been queried (including the current query).- For example, when the system is queried for the
4^thtime, it returns the4^thbest location of all locations already added.
- For example, when the system is queried for the
Note that the test data are generated so that at any time, the number of queries does not exceed the number of locations added to the system.
Implement the SORTracker class:
SORTracker()Initializes the tracker system.void add(string name, int score)Adds a scenic location withnameandscoreto the system.string get()Queries and returns thei^thbest location, whereiis the number of times this method has been invoked (including this invocation).
Example 1:
Input
["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"]
[[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]
Output
[null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]
Explanation
SORTracker tracker = new SORTracker(); // Initialize the tracker system.
tracker.add("bradford", 2); // Add location with name="bradford" and score=2 to the system.
tracker.add("branford", 3); // Add location with name="branford" and score=3 to the system.
tracker.get(); // The sorted locations, from best to worst, are: branford, bradford.
// Note that branford precedes bradford due to its higher score (3 > 2).
// This is the 1^st time get() is called, so return the best location: "branford".
tracker.add("alps", 2); // Add location with name="alps" and score=2 to the system.
tracker.get(); // Sorted locations: branford, alps, bradford.
// Note that alps precedes bradford even though they have the same score (2).
// This is because "alps" is lexicographically smaller than "bradford".
// Return the 2^nd best location "alps", as it is the 2^nd time get() is called.
tracker.add("orland", 2); // Add location with name="orland" and score=2 to the system.
tracker.get(); // Sorted locations: branford, alps, bradford, orland.
// Return "bradford", as it is the 3^rd time get() is called.
tracker.add("orlando", 3); // Add location with name="orlando" and score=3 to the system.
tracker.get(); // Sorted locations: branford, orlando, alps, bradford, orland.
// Return "bradford".
tracker.add("alpine", 2); // Add location with name="alpine" and score=2 to the system.
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "bradford".
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "orland".
Constraints:
nameconsists of lowercase English letters, and is unique among all locations.1 <= name.length <= 101 <= score <= 10^5- At any time, the number of calls to
getdoes not exceed the number of calls toadd. - At most
4 * 10^4calls in total will be made toaddandget.
Solution
Approach: Sorted list
Keep every location in a list ordered from best to worst, that is by higher score first and then by smaller name, and keep a counter i of the get calls so far.
addfinds the insertion point with binary search and inserts there.getreturns the element at positioniand advancesi.
With at most 4 * 10^4 operations, the O(n) shift during an insert is cheap. A balanced tree with order statistics, or two heaps (one for the locations already returned, one for the rest), brings add down to O(log n).
Complexity: get is O(1); add is O(log n) to search plus O(n) to shift; space O(n).
import bisect
class SORTracker:
def __init__(self):
self.items = [] # sorted by (-score, name), best first
self.i = 0
def add(self, name: str, score: int) -> None:
bisect.insort(self.items, (-score, name))
def get(self) -> str:
self.i += 1
return self.items[self.i - 1][1]
# Your SORTracker object will be instantiated and called as such:
# obj = SORTracker()
# obj.add(name,score)
# param_2 = obj.get()