Count of days remaining for the next day with higher temperature | 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 ; Given temperatures ; Function Call
function dailyTemperatures ( T ) { var n = T . length ; var daysOfWait = Array ( n ) . fill ( - 1 ) ; var s = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { while ( s . length != 0 && T [ s [ s . length - 1 ] ] < T [ i ] ) { daysOfWait [ s [ s . length - 1 ] ] = i - s [ s . length - 1 ] ; s . pop ( ) ; } s . push ( i ) ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( daysOfWait [ i ] + " " ) ; } } var arr = [ 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 ] ; dailyTemperatures ( arr ) ;