算法训练营第十一天|150. 逆波兰表达式求值、239. 滑动窗口最大值、347.前 K 个高频元素

150. 逆波兰表达式求值

题目

思路与解法

第一思路: 比较简单

python 复制代码
class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        for item in tokens:
            if item != '+' and item != '-' and item != '*' and item != '/' :
                stack.append(item)
            else:
                b = int(stack.pop())
                a = int(stack.pop())
                if item == '+':
                    stack.append(a + b)
                elif item == '-':
                    stack.append(a - b)
                elif item == '*':
                    stack.append(a * b)
                elif item == '/':
                    stack.append(a/b)
        return int(stack.pop())

239. 滑动窗口最大值

题目

思路与解法

第一思路:

carl的思路 : 滑动窗口解法,值得后面认真看看细节,归纳一下

python 复制代码
from collections import deque
class MyQueue(object): # 单调队列
    
    def __init__(self):
        self.queue = deque()

    def pop(self, value):
        if self.queue and value == self.queue[0]:
            return self.queue.popleft()

    def push(self, value):
        while self.queue and value > self.queue[-1]:
            self.queue.pop()
        self.queue.append(value)

    def front(self):
        return self.queue[0]


class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        myque = MyQueue()
        res = []
        lens = len(nums)
        i=0
        if i + k <= lens:
            while i < k:
                myque.push(nums[i])
                i += 1

        res.append(myque.front())
        i=1
        while i + k <= lens:
            myque.pop(nums[i-1])
            myque.push(nums[i+k-1])
            res.append(myque.front())
            i += 1
        return res

347.前 K 个高频元素

题目

思路与解法

第一思路: 先统计数量,在得出前k多的值。但是不知道怎么实现。有点被误导,因为要很技巧性,其实感觉很粗暴
carl的讲解: 先用字典统计,再将字典key-value互换,然后将互换后的keys(values)方法到list中,进行排序,得出按顺序的出现次数,再通过出现次数去找对应的值。

python 复制代码
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        from collections import defaultdict

        res = []

        item_dict = defaultdict(int)
        for item in nums:
            item_dict[item] += 1
        
        time_dict = defaultdict(list) 
        for key in item_dict.keys():
            time_dict[item_dict[key]].append(key)
        
        times = time_dict.keys()
        times = list(times)
        times.sort() # 从小往大
        count = 0 # 记载存入res中的总个数

        while count < k:
            res.extend(time_dict[times[-1]])
            count += len(time_dict[times[-1]])
            times.pop()
        
        return res
相关推荐
智者知已应修善业4 小时前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
地平线开发者4 小时前
PTQ 量化数值范围与优化
算法·自动驾驶
sali-tec4 小时前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
测试人社区-小明4 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
罗西的思考5 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
qq_433554548 小时前
C++数位DP
c++·算法·图论
AshinGau8 小时前
Softmax 与 交叉熵损失
神经网络·算法
似水এ᭄往昔8 小时前
【C++】--AVL树的认识和实现
开发语言·数据结构·c++·算法·stl
栀秋6669 小时前
“无重复字符的最长子串”:从O(n²)哈希优化到滑动窗口封神,再到DP降维打击!
前端·javascript·算法
xhxxx9 小时前
不用 Set,只用两个布尔值:如何用标志位将矩阵置零的空间复杂度压到 O(1)
javascript·算法·面试