Count of days remaining for the next day with higher temperature | C ++ program for the above approach ; Function to determine how many days required to wait for the next warmer temperature ; To store the answer ; Traverse all the temperatures ; Check if current index is the next warmer temperature of any previous indexes ; Pop the element ; Push the current index ; Print waiting days ; Driver Code ; Given temperatures ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dailyTemperatures ( vector < int > & T ) { int n = T . size ( ) ; vector < int > daysOfWait ( n , -1 ) ; stack < int > s ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! s . empty ( ) && T [ s . top ( ) ] < T [ i ] ) { daysOfWait [ s . top ( ) ] = i - s . top ( ) ; s . pop ( ) ; } s . push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { cout << daysOfWait [ i ] << " ▁ " ; } } int main ( ) { vector < int > arr { 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 } ; dailyTemperatures ( arr ) ; return 0 ; }