CSES - Different repeats

Implement a class TrackRepeat with the following methods:

  • add(x, k): add the number x to the list k times
  • check(): return True if each number on the list has a different number of occurrences, and otherwise False

The time complexity of both methods should be O(1)O(1).

For example, the list [1,3,1,1,2,3,1][1,3,1,1,2,3,1] contains three distinct numbers 11, 22 and 33. The number 11 occurs 44 times, the number 22 occurs 11 times, and the number 33 occurs 22 times. Thus each number has a different number of occurrences (44, 11 and 22 occurrences).

In a file trackrepeat.py, implement a class TrackRepeat according to the following template:

class TrackRepeat:
    def __init__(self):
        # TODO

    def add(self, x, k):
        # TODO

    def check(self):
        # TODO

if __name__ == "__main__":
    t = TrackRepeat()
    print(t.check()) # True
    t.add(1, 3)
    print(t.check()) # True
    t.add(2, 3)
    print(t.check()) # False
    t.add(2, 2)
    print(t.check()) # True
    t.add(3, 1)
    print(t.check()) # True
    t.add(3, 4)
    print(t.check()) # False