Given a list arr[] of everyday temperatures. For each day, the task is to find the count of days remaining for the next day with warmer temperatures. If there is no such day for which warmer temperature is possible then print -1.
Examples:
Input: arr[] = {73, 74, 75, 71, 69, 72, 76, 73}
Output: {1, 1, 4, 2, 1, 1, -1, -1}
Explanation:
For 73 temperature, next warmer temperature is 74 which at a distance 1
For 74 temperature, next warmer temperature is 75 which at a distance 1
For 75 temperature, next warmer temperature is 76 which at a distance 4
For 71 temperature, next warmer temperature is 72 which at a distance 2
For 69 temperature, next warmer temperature is 72 which at a distance 1
For 72 temperature, next warmer temperature is 76 which at a distance 1
For 76 temperature, there is no valid next warmer day
For 73 temperature, there is no valid next warmer dayInput: arr[] = {75, 74, 73, 72}
Output: {-1, -1, -1, -1}
Naive Approach: The idea is to iterate for each possible pairs of the array and check the next greater temperatures for each current element.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: This problem basically asks to find how far is the current index from the index of next greater temperature to the temperature at the current index. The most optimal way to solve this problem is by making use of a stack. Below are the steps:
- Iterate over the everyday temperature of the given array arr[] using the current index.
- If the stack is empty, push the current index to the stack.
- If the stack is not empty then do the following:
- If the temperature at the current index is lesser than the temperature of the index at top of the stack, push the current index.
- If the temperature at the current index is greater than the temperature of the index at top of the stack, then update the no of days to wait for warmer temperature as:
current index – index at top of the stack
- Pop the stack once the number of days has been updated in the output list.
- Repeat the above steps for all the indices in the stack that are lesser than the temperature at the current index.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to determine how many days // required to wait for the next // warmer temperature void dailyTemperatures(vector< int >& T) { int n = T.size(); // To store the answer vector< int > daysOfWait(n, -1); stack< int > s; // Traverse all the temperatures for ( int i = 0; i < n; i++) { // Check if current index is the // next warmer temperature of // any previous indexes while (!s.empty() && T[s.top()] < T[i]) { daysOfWait[s.top()] = i - s.top(); // Pop the element s.pop(); } // Push the current index s.push(i); } // Print waiting days for ( int i = 0; i < n; i++) { cout << daysOfWait[i] << " " ; } } // Driver Code int main() { // Given temperatures vector< int > arr{ 73, 74, 75, 71, 69, 72, 76, 73 }; // Function Call dailyTemperatures(arr); return 0; } |
Java
// Java program for the above approach import java.util.*; import java.lang.*; class GFG{ // Function to determine how many days // required to wait for the next // warmer temperature static void dailyTemperatures( int [] T) { int n = T.length; // To store the answer int [] daysOfWait = new int [n]; Arrays.fill(daysOfWait, - 1 ); Stack<Integer> s = new Stack<>(); // Traverse all the temperatures for ( int i = 0 ; i < n; i++) { // Check if current index is the // next warmer temperature of // any previous indexes while (!s.isEmpty() && T[s.peek()] < T[i]) { daysOfWait[s.peek()] = i - s.peek(); // Pop the element s.pop(); } // Push the current index s.push(i); } // Print waiting days for ( int i = 0 ; i < n; i++) { System.out.print(daysOfWait[i] + " " ); } } // Driver Code public static void main (String[] args) { // Given temperatures int [] arr = { 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 }; // Function call dailyTemperatures(arr); } } // This code is contributed by offbeat |
Python3
# Python3 program for the above approach # Function to determine how many days # required to wait for the next # warmer temperature def dailyTemperatures(T): n = len (T) # To store the answer daysOfWait = [ - 1 ] * n s = [] # Traverse all the temperatures for i in range (n): # Check if current index is the # next warmer temperature of # any previous indexes while ( len (s) ! = 0 and T[s[ - 1 ]] < T[i]): daysOfWait[s[ - 1 ]] = i - s[ - 1 ] # Pop the element s.pop( - 1 ) # Push the current index s.append(i) # Print waiting days for i in range (n): print (daysOfWait[i], end = " " ) # Driver Code # Given temperatures arr = [ 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 ] # Function call dailyTemperatures(arr) # This code is contributed by Shivam Singh |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to determine how many days // required to wait for the next // warmer temperature static void dailyTemperatures( int [] T) { int n = T.Length; // To store the answer int [] daysOfWait = new int [n]; for ( int i = 0; i < n; i++) daysOfWait[i] = -1; Stack< int > s = new Stack< int >(); // Traverse all the temperatures for ( int i = 0; i < n; i++) { // Check if current index is the // next warmer temperature of // any previous indexes while (s.Count != 0 && T[s.Peek()] < T[i]) { daysOfWait[s.Peek()] = i - s.Peek(); // Pop the element s.Pop(); } // Push the current index s.Push(i); } // Print waiting days for ( int i = 0; i < n; i++) { Console.Write(daysOfWait[i] + " " ); } } // Driver Code public static void Main(String[] args) { // Given temperatures int [] arr = { 73, 74, 75, 71, 69, 72, 76, 73 }; // Function call dailyTemperatures(arr); } } // This code is contributed by Amit Katiyar |
1 1 4 2 1 1 -1 -1
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.