Back to Practice Dashboard
Top 150 InterviewEasy

Daily Temperatures

Learn how to solve the 'Daily Temperatures' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

Write a function dailyTemperatures(temperatures: List[int]) -> List[int].

Constraints
  • 1 <= len(temperatures) <= 10^5
  • 30 <= temperatures[i] <= 100

Examples

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?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor