You are designing a system to track the ranking of scenic locations. Each location has a unique name
(string) and an attractiveness score
(integer). Locations are ranked from best to worst based on their score (higher is better). If two locations have the same score, the location with the lexicographically smaller name is considered better.
Initially, the system has no locations. You need to implement the SORTracker
class with the following functionalities:
SORTracker()
: Initializes the tracker system.void add(string name, int score)
: Adds a scenic location with the given name
and score
to the system.string get()
: Queries and returns the i-th best location, where i
is the number of times this method has been invoked (including the current invocation). For example, if get()
is called for the 4th time, it should return the 4th best location among all locations added so far.Example:
SORTracker tracker = new SORTracker();
tracker.add("bradford", 2);
tracker.add("branford", 3);
tracker.get(); // Returns "branford" (1st call)
tracker.add("alps", 2);
tracker.get(); // Returns "alps" (2nd call)
tracker.add("orland", 2);
tracker.get(); // Returns "bradford" (3rd call)
Constraints:
name
consists of lowercase English letters and is unique.1 <= name.length <= 10
1 <= score <= 10^5
get
will not exceed the number of calls to add
at any time.add
and get
will be at most 4 * 10^4.How would you implement the SORTracker
class to efficiently handle these operations?
A naive solution involves storing all the locations in a list and sorting the list each time the get()
method is called. This approach is simple to implement but inefficient due to the sorting operation.
add(name, score)
: Append the new location (name, score) to a list of locations.get()
: Sort the list of locations based on the score (descending) and name (lexicographically ascending if scores are equal). Return the i-th element, where i is the number of times get()
has been called.add()
: O(1)get()
: O(N log N), where N is the number of locations added so far.An optimal solution uses two data structures to maintain the sorted order of the locations efficiently:
add(name, score)
: Insert the new location (name, score) into the sorted list while maintaining the sorted order.get()
: Return the location at the current index. Increment the index for the next call.add(name, score)
: Add the new location to the list. Sort the list based on the specified criteria (score descending, name ascending if scores are equal).get()
: Increment a counter (initialized to 0) each time get()
is called. Return the element at the counter-th index in the sorted list.class SORTracker:
def __init__(self):
self.locations = []
self.i = 0
def add(self, name: str, score: int) -> None:
self.locations.append((name, score))
self.locations.sort(key=lambda x: (-x[1], x[0]))
def get(self) -> str:
result = self.locations[self.i][0]
self.i += 1
return result
add()
: O(N log N) due to the sorting after each insertion.get()
: O(1)get()
method should handle the empty case gracefully (though the prompt specifies that the number of get
calls will never exceed the number of add
calls).