본문 바로가기

IT/코딩테스트

[Leetcode] 2671. Frequency Tracker

728x90

 

class FrequencyTracker:

    def __init__(self):
        self.data = {}
        self.freq = {}

    def add(self, number: int) -> None:
        if self.data.get(number, 0) != 0:
            self.freq[self.data[number]] = self.freq.get(self.data[number], 1) - 1

        self.data[number] = self.data.get(number, 0) + 1
        self.freq[self.data[number]] = self.freq.get(self.data[number], 0) + 1

    def deleteOne(self, number: int) -> None:
        if self.data.get(number, 0) == 0:
            return
        
        self.freq[self.data[number]] = self.freq.get(self.data[number], 1) - 1

        self.data[number] = self.data.get(number, 1) - 1
        self.freq[self.data[number]] = self.freq.get(self.data[number], 0) + 1

    def hasFrequency(self, frequency: int) -> bool:
        if self.freq.get(frequency, 0) > 0:
            return True

        return False


# Your FrequencyTracker object will be instantiated and called as such:
# obj = FrequencyTracker()
# obj.add(number)
# obj.deleteOne(number)
# param_3 = obj.hasFrequency(frequency)

https://leetcode.com/problems/frequency-tracker/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

728x90