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/
728x90
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 771. Jewels and Stones (0) | 2024.02.07 |
---|---|
[Leetcode] 1512. Number of Good Pairs (0) | 2024.02.07 |
[Leetcode] 2236. Root Equals Sum of Children (0) | 2024.02.05 |
[Leetcode] 1002. Find Common Characters (0) | 2024.02.04 |
[Leetcode] 2703. Return Length of Arguments Passed (0) | 2024.02.02 |