Similar Problems

Similar Problems not available

Sequentially Ordinal Rank Tracker - Leetcode Solution

Companies:

LeetCode:  Sequentially Ordinal Rank Tracker Leetcode Solution

Difficulty: Hard

Topics: design heap-priority-queue  

The Sequentially Ordinal Rank Tracker problem is a coding challenge on LeetCode that requires you to implement a class that can keep track of a set of integers and rank them in sequential, ordinal order. The class should have the following methods:

  • track(x): Add an integer x to the set of tracked integers
  • getRankOfNumber(x): Get the sequential ordinal rank of integer x in the set of tracked integers
  • getNumbersAtRank(rank): Get the set of integers with a sequential ordinal rank of rank

To solve this problem, you will need to implement a data structure that can efficiently store and manipulate the set of tracked integers. Here is one possible solution:

class SequentiallyOrdinalRankTracker:
    def __init__(self):
        self.tracked_nums = []
        
    def track(self, x):
        self.tracked_nums.append(x)
        self.tracked_nums.sort()
        
    def getRankOfNumber(self, x):
        rank = 0
        for num in self.tracked_nums:
            if num < x:
                rank += 1
            elif num == x:
                return rank
            else:
                break
        return rank
        
    def getNumbersAtRank(self, rank):
        return self.tracked_nums[rank]

In this solution, we use a simple list to store the set of tracked integers. When we add a new integer to the set using the track method, we simply append it to the end of the list and then sort the list in ascending order.

To get the sequential ordinal rank of a particular integer x in the set, we iterate over the list of tracked integers and compare each one to x. If the tracked integer is less than x, we increment the rank counter. If the tracked integer is equal to x, we return the current rank counter. If the tracked integer is greater than x, we break out of the loop and return the current rank counter.

To get the set of integers with a particular sequential ordinal rank rank, we simply return the value at the rank index of the sorted list of tracked integers.

Overall, this solution has time complexity O(n log n) for the track method (due to the sorting operation) and O(n) for the getRankOfNumber and getNumbersAtRank methods (due to the linear search through the list of tracked integers). There are more efficient data structures that could be used to improve the time complexity of this solution, but this solution should be sufficient for most use cases.

Sequentially Ordinal Rank Tracker Solution Code

1