150强访谈简单

每日气温

“每日温度”问题的详细指南和 Python 实现。

问题陈述

简单

给定一个整数数组 temperatures 表示每日温度,返回一个数组 answer ,其中 answer[i] 是在第 i 天之后您必须等待的天数才能获得较温暖的温度。如果未来没有一天可以这样做,请保留 answer[i] == 0

编写一个函数 dailyTemperatures(temperatures: List[int]) -> List[int]

约束条件
  • 1 <= len(temperatures) <= 10^5
  • 30 <= temperatures[i] <= 100

示例

Example 1
Input
temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output
[1, 1, 4, 2, 1, 1, 0, 0]
Explanation

Day 0 (73): next warmer is day 1 (74), wait 1 day. Day 2 (75): next warmer is day 6 (76), wait 4 days. Days 6 and 7 have no warmer future day.

Example 2
Input
temperatures = [30, 40, 50, 60]
Output
[1, 1, 1, 0]
Explanation

Each day except the last has a warmer day immediately after.

Example 3
Input
temperatures = [30, 60, 90]
Output
[1, 1, 0]
Explanation

Temperatures are strictly increasing except the last day.

Need a Hint?
考虑使用特定于堆栈的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。