BackmediumStackInfosys

Daily Temperature Variations Solution

Problem Statement

Given a list of daily temperatures, return a list of the number of days until a warmer temperature occurs. If no warmer temperature occurs, return 0 for that day.

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

Explanation: Step-by-step: For the given input [73, 74, 75, 71, 69, 72, 76, 73], we start from the first element 73. The next warmer day is 74, so the answer is 1. Then for 74, the next warmer day is 75, so the answer is 1. Then for 75, the next warmer day is 76, so the answer is 1. Then for 76, there is no warmer day, so the answer is 0. We continue this process for each day.

Example 2
Input
[89, 62, 70, 58, 59, 90, 75, 85, 73, 72, 60, 74]
Output
[1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 0]

Explanation: Step-by-step: For the given input [89, 62, 70, 58, 59, 90, 75, 85, 73, 72, 60, 74], we start from the first element 89. The next warmer day is 90, so the answer is 1. Then for 90, there is no warmer day, so the answer is 0. We continue this process for each day.

Constraints

  • Array size ≤ 10^5
  • All temperatures are between -10^8 and 10^8
  • No duplicate temperatures in the array
  • All elements in the array are integers
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Daily Temperature Variations — Problem Statement & Solution Guide

StackMediumMonotonic Stack
TimeO(n)
|
SpaceO(n)

Problem Description

Given a list of daily temperatures, return a list of the number of days until a warmer temperature occurs. If no warmer temperature occurs, return 0 for that day.

Examples

Example 1

Input

[73, 74, 75, 71, 69, 72, 76, 73]

Output

[1, 1, 4, 2, 1, 1, 0, 0]

Explanation: Step-by-step: For the given input [73, 74, 75, 71, 69, 72, 76, 73], we start from the first element 73. The next warmer day is 74, so the answer is 1. Then for 74, the next warmer day is 75, so the answer is 1. Then for 75, the next warmer day is 76, so the answer is 1. Then for 76, there is no warmer day, so the answer is 0. We continue this process for each day.

Example 2

Input

[89, 62, 70, 58, 59, 90, 75, 85, 73, 72, 60, 74]

Output

[1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 0]

Explanation: Step-by-step: For the given input [89, 62, 70, 58, 59, 90, 75, 85, 73, 72, 60, 74], we start from the first element 89. The next warmer day is 90, so the answer is 1. Then for 90, there is no warmer day, so the answer is 0. We continue this process for each day.

Constraints

  • Array size ≤ 10^5
  • All temperatures are between -10^8 and 10^8
  • No duplicate temperatures in the array
  • All elements in the array are integers

Optimal Approach & Strategy

We can use a monotonic stack to optimize the solution. The stack will store the indices of the temperatures. Whenever a warmer temperature is found, we pop the stack and update the result array. This approach reduces the time complexity to O(n).

Brute Force Approach

One possible approach is to iterate through the array for each element and find the next warmer day by comparing with all the following elements. However, this approach has a time complexity of O(n²) due to the nested loops.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function dailyTemperatures(temperatures) {
  const n = temperatures.length;
  const stack = [];
  const result = new Array(n).fill(0);

  for (let i = 0; i < n; i++) {
    while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
      const top = stack.pop();
      result[top] = i - top;
    }
    stack.push(i);
  }

  return result;
}

Asked in Top Tech Interviews

Infosys

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.