'LeetCode Problem 895 (Python), can't finish last test
Link to problem I've been trying to solve this problem and have come very close. I'm looking to fix the push method, but for the last call:
freq_stack = [5, 7, 4]
freq_stack.pop()
It returns [5, 4], not [5, 7]
Here is my code:
class FreqStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
self.stack.append(val)
def pop(self) -> int:
mock_stack = self.stack[:]
unique_nums = []
while True:
if len(mock_stack) != 1:
for num in mock_stack:
if num not in unique_nums:
mock_stack.remove(num)
unique_nums.append(num)
unique_nums.clear()
else: break
most_freq_num = mock_stack[0]
reversed_stack = self.stack[::-1]
reversed_stack.remove(most_freq_num)
self.stack = reversed_stack[::-1] # After removing most freqent item, update stack.
return most_freq_num
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
