class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
# sm=0
for i in tokens:
if i in ['+', '-', '*', '/']:
b = int(stack.pop())
a = int(stack.pop())
if i == '+':
stack.append(a + b)
elif i == '-':
stack.append(a - b)
elif i == '*':
stack.append(a * b)
else:
stack.append(int(a / b))
else:
stack.append(int(i))
print(stack)
return stack.pop()
from collections import deque
class myque:
def __init__(self):
self.que=deque()
def push(self,v):
while self.que and self.que[-1]<v:
self.que.pop()
self.que.append(v)
def pop(self,v):
if self.que and self.que[0]==v:
self.que.popleft()
def get_max(self):
return self.que[0]
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
que=myque()
re=[]
for i in range(k):
que.push(nums[i])
re.append(que.get_max())
for i in range(k,len(nums)):
que.pop(nums[i-k])
que.push(nums[i])
re.append(que.get_max())
return re
import heapq
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
mapp = {}
for i in nums:
mapp[i] = mapp.get(i, 0) + 1
hpq = []
for i in mapp.keys():
heapq.heappush(hpq, (mapp[i], i))
if len(hpq) > k:
heapq.heappop(hpq)
re = [0] * k
for i in range(k):
re[k - i - 1] = heapq.heappop(hpq)[1]
return re