739. 每日温度 - 力扣(LeetCode)

方法1:暴力法

  • 时间:O(n²) ------ 最坏情况(递减序列)需比较 n(n-1)/2 次
  • 空间:O(1) ------ 仅输出数组
python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 11:04 
# "Stay hungry,stay foolish."

class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        # 2026 - 1 - 7
        result = [0] * len(temperatures)
        for i in range(len(temperatures)):
            for j in range(i+1, len(temperatures)):
                if temperatures[i] < temperatures[j]:
                    result[i] = j - i
                    break

        return result

方法2:预处理 + 逆序优化(超有趣,本题的最强算法)

  • 时间 :平均 O(n),最坏 O(n²)(如 [5,4,3,2,1,6]
  • 空间:O(1)
python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 11:04 
# "Stay hungry,stay foolish."

class Solution(object):
    def __init__(self, temperatures):
        self.temperatures = temperatures
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        n = len(temperatures)
        answer = [0] * n

        # 从倒数第二天开始(最后一天永远是0)
        for i in range(n - 2, -1, -1):
            j = i + 1
            while j < n and temperatures[j] <= temperatures[i]:
                if answer[j] == 0:  # 后面没有更大的了
                    j = n  # 跳出循环
                    break
                j += answer[j]  # 跳到下一个可能更大的位置
            if j < n:
                answer[i] = j - i
        return answer

sol = Solution([73,74,75,71,69,72,76,73])
print(sol.dailyTemperatures([73,74,75,71,69,72,76,73]))

# i = 6(76)
# j = 7 → 73 ≤ 76
# answer[7] == 0 → break
# 👉 answer[6] = 0
#
# i = 5(72)
# j = 6 → 76 > 72
# 👉 找到!
# answer[5] = 6 - 5 = 1
#
# i = 4(69)
# j = 5 → 72 > 69
# 👉 answer[4] = 1
#
# i = 3(71)
# j = 4 → 69 ≤ 71
# answer[4] = 1 → j = 4 + 1 = 5
# j = 5 → 72 > 71
# 👉 answer[3] = 5 - 3 = 2
#
# i = 2(75)
# j = 3 → 71 ≤ 75
# answer[3] = 2 → j = 3 + 2 = 5
# j = 5 → 72 ≤ 75
# answer[5] = 1 → j = 5 + 1 = 6
# j = 6 → 76 > 75
# 👉 answer[2] = 4

方法3:单调栈(标准算法)

  • 时间:O(n) ------ 每个元素入栈出栈各一次
  • 空间:O(n) ------ 栈最多存 n 个索引
python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 11:04 
# "Stay hungry,stay foolish."

class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        # 2026 - 1 - 7
        result = [0] * len(temperatures)
        stack = [] # [temp,index]
        for i, temp in enumerate(temperatures):
            while stack and temp > stack[-1][0]:
                stackT, stackInd = stack.pop()
                result[stackInd] = i - stackInd
            stack.append((temp, i))
        return result

结果

解题步骤:www.bilibili.com

相关推荐
minji...1 小时前
Linux 进程信号(二)信号的保存,sigset_t,sigprocmask,sigpending
linux·运维·服务器·网络·数据结构·c++·算法
庞轩px1 小时前
模拟面试回答第十三问:JVM内存模型
jvm·面试·职场和发展
罗湖老棍子1 小时前
最大数(信息学奥赛一本通- P1549)(洛谷-P1198)
数据结构·算法·线段树·单点修改 区间求最大值
小O的算法实验室3 小时前
2026年KBS,赏金猎人优化算法+多无人机移动边缘计算与路径规划,深度解析+性能实测
算法·无人机·边缘计算
用户5671504710213 小时前
OpenClaw 记忆管理系统技术文档
算法
935963 小时前
练习题53-60
算法·深度优先
霖大侠3 小时前
Wavelet Meets Adam: Compressing Gradients forMemory-Efficient Training
人工智能·深度学习·算法·机器学习·transformer
AI成长日志4 小时前
【笔面试算法学习专栏】二分查找专题:力扣hot100经典题目深度解析
学习·算法·面试
lcreek4 小时前
流量优化之道:Ford-Fulkerson 最大流算法
算法·
垫脚摸太阳4 小时前
第 36 场 蓝桥·算法挑战赛·百校联赛---赛后复盘
数据结构·c++·算法