代码随想录第48天

739. 每日温度

python 复制代码
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures)
        ans = [0] * n
        st = []
        for i in range(n - 1, -1, -1):
            t = temperatures[i]
            while st and t >= temperatures[st[-1]]:
                st.pop()
            if st:
                ans[i] = st[-1] - i
            st.append(i)
        return ans

496.下一个更大元素 I

python 复制代码
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        idx = {x: i for i, x in enumerate(nums1)}
        ans = [-1] * len(nums1)
        st = []
        for x in reversed(nums2):
            while st and x >= st[-1]:
                # 由于 x 的出现,栈顶元素永远不会是左边元素的「下一个更大元素」
                st.pop()
            if st and x in idx:  # x 在 nums1 中
                ans[idx[x]] = st[-1]  # 记录答案
            st.append(x)
        return ans

503.下一个更大元素II

python 复制代码
class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        n = len(nums)
        ans = [-1] * n
        st = []
        for i in range(n * 2 - 1, -1, -1):
            x = nums[i % n]
            while st and x >= st[-1]:
                # 由于 x 的出现,栈顶元素永远不会是左边元素的「下一个更大元素」
                st.pop()
            if st and i < n:
                ans[i] = st[-1]
            st.append(x)
        return ans
相关推荐
m***66736 分钟前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
I_ltt_Itw,15 分钟前
Python多线程
服务器·python·pycharm
Hello.Reader17 分钟前
Flink SQL 数据类型从 INT 到 VARIANT 的完整实战指南
python·sql·flink
vi1212331 分钟前
ENVI 地形量化与植被指数反演
开发语言·python
rising start33 分钟前
一、FastAPI入门
python·fastapi·端口
闲人编程39 分钟前
Flask应用工厂模式:构建可扩展的大型应用
后端·python·flask·工厂模式·codecapsule·应用工厂
LitchiCheng1 小时前
Mujoco 检验 KDL 和 Pinocchio 运动学 FK 是否一致
人工智能·python
ZhengEnCi1 小时前
P3H1-Python-sys模块完全指南-系统参数与命令行参数处理利器
python
猫头虎1 小时前
如何解决pip install网络报错SSLError: TLSV1_ALERT_PROTOCOL_VERSION(OpenSSL过旧)问题
网络·python·scrapy·pycharm·beautifulsoup·pip·scipy
帮帮志1 小时前
05【AI大模型对话/创建项目】通过pycharm创建大模型项目,关联Anaconda环境
ide·人工智能·python·语言模型·pycharm