BackmediumStack Flipkart

Daily Temperatures 2 Solution

Problem Statement

Given an array of daily solar power output temperatures, find how many days until there is a more productive (higher output) day. Output 0 if no such future day.

Example 1
Input
[65, 70, 55, 60, 50, 75]
Output
[0, 0, 0, 0, 0, 1]

Explanation: Step-by-step: Given the input array [65, 70, 55, 60, 50, 75], we start by initializing an empty stack and an output array of the same length as the input array. We then iterate through the input array. For the first element 65, we push the index 0 onto the stack and append 0 to the output array. For the second element 70, we push the index 1 onto the stack and append 0 to the output array. For the third element 55, we pop the index 0 from the stack and append the difference between the current temperature and the temperature at index 0 to the output array. We repeat this process for the remaining elements in the input array. The final output array is [0, 0, 15, 0, 40, 1].

Example 2
Input
[50, 40, 30, 20, 10]
Output
[0, 0, 0, 0, 0]

Explanation: Step-by-step: Given the input array [50, 40, 30, 20, 10], we start by initializing an empty stack and an output array of the same length as the input array. We then iterate through the input array. For the first element 50, we push the index 0 onto the stack and append 0 to the output array. For the second element 40, we push the index 1 onto the stack and append 0 to the output array. For the third element 30, we push the index 2 onto the stack and append 0 to the output array. For the fourth element 20, we push the index 3 onto the stack and append 0 to the output array. For the fifth element 10, we pop all indices from the stack and append the difference between the current temperature and the temperature at each index to the output array. The final output array is [0, 0, 0, 0, 40].

Constraints

  • 1 <= n <= 10^5
  • 30 <= temp[i] <= 100
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free