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 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。