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 day
Input: 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:
1. Iterate over the everyday temperature of the given array arr[] using the current index.
2. If the stack is empty, push the current index to the stack.
3. 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.
4. 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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static void dailyTemperatures( int [] T)
{
int n = T.length;
int [] daysOfWait = new int [n];
Arrays.fill(daysOfWait, - 1 );
Stack<Integer> s = new Stack<>();
for ( int i = 0 ; i < n; i++)
{
while (!s.isEmpty() && T[s.peek()] < T[i])
{
daysOfWait[s.peek()] = i - s.peek();
s.pop();
}
s.push(i);
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(daysOfWait[i] + " " );
}
}
public static void main (String[] args)
{
int [] arr = { 73 , 74 , 75 , 71 ,
69 , 72 , 76 , 73 };
dailyTemperatures(arr);
}
}
|
Python3
def dailyTemperatures(T):
n = len (T)
daysOfWait = [ - 1 ] * n
s = []
for i in range (n):
while ( len (s) ! = 0 and
T[s[ - 1 ]] < T[i]):
daysOfWait[s[ - 1 ]] = i - s[ - 1 ]
s.pop( - 1 )
s.append(i)
for i in range (n):
print (daysOfWait[i], end = " " )
arr = [ 73 , 74 , 75 , 71 ,
69 , 72 , 76 , 73 ]
dailyTemperatures(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void dailyTemperatures( int [] T)
{
int n = T.Length;
int [] daysOfWait = new int [n];
for ( int i = 0; i < n; i++)
daysOfWait[i] = -1;
Stack< int > s = new Stack< int >();
for ( int i = 0; i < n; i++)
{
while (s.Count != 0 && T[s.Peek()] < T[i])
{
daysOfWait[s.Peek()] = i - s.Peek();
s.Pop();
}
s.Push(i);
}
for ( int i = 0; i < n; i++)
{
Console.Write(daysOfWait[i] + " " );
}
}
public static void Main(String[] args)
{
int [] arr = { 73, 74, 75, 71,
69, 72, 76, 73 };
dailyTemperatures(arr);
}
}
|
Javascript
<script>
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);
</script>
|
Output:
1 1 4 2 1 1 -1 -1
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!