Sobes.tech
Junior — Senior

Getting N largest elements from a dynamic array

livecode

Task condition

Implement two methods. The first method takes a number and adds it to an internal array, the second returns the N largest values from this array.

class TopNumbers:
    def __init__(self):
        self.storage = []

    def add(self, value: int) -> None:
        # add a number to the list
        self.storage.append(value)

    def get_top(self, n: int) -> list[int]:
        # return the n largest numbers
        return sorted(self.storage, reverse=True)[:n]