Table of contents
Hello and welcome back to another day of struggling with Leetcode! Let's get started without further ado.
Understanding the Problem
Take a look at the problem statement given below.
Given an array of integers
temperatures
that represents the daily temperatures, return an arrayanswer
such thatanswer[i]
is the number of days you have to wait after theith
day to get a warmer temperature. If there is no future day for which this is possible, keepanswer[i] == 0
instead.
This problem is similar to Next Greater Element, a Leetcode classic.
The description is pretty simple, all we need to do is find the distance of the next greater element for each element in the array. Let's look at a few examples to solidify our understanding of the problem.
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Input: temperatures = [30,60,90]
Output: [1,1,0]
Let's see how we can solve this problem now, shall we?
Intuition
We need to use a stack for solving this problem. This is suggested by the fact that we need to access the most recent elements to find the next greater element for each of them.
The process consists of the following steps:
Initialize the result array with size n and all elements set to 0.
Declare a stack of type
pair<int,int>
to store the (index, value) pair.Loop through the input array. For each element, check if:
The stack is empty OR the current element is less than the element at the top of the stack.
Else, if the current element is greater than the element at the top of the stack.
In the first case, we push the current element and its index to the stack.
In the second case, we pop elements from the stack until the element at the top of the stack is greater than the current element or the stack becomes empty.
After popping each element, we update the index of the popped element in the result array with the calculated distance.
That's all we need to do in order to solve the problem!
Coding the Solution
Here's the full solution for the problem.
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temp) {
int n = temp.size();
vector<int> ans(n,0);
//store index,value in stack
stack<pair<int,int>> s;
//iterate through array
for(int i = 0;i < n; i++){
//if stack is empty or if current element is less than top of stack then push
if(s.empty() or s.top().second >= temp[i]){
s.push({i,temp[i]});
}
//else if current element is greater than top of stack, pop until top of stack is greater than current element or stack is empty
else{
while(!s.empty() and s.top().second < temp[i]){
pair<int,int> p = s.top();
s.pop();
ans[p.first] = i - p.first;
}
s.push({i,temp[i]});
}
}
return ans;
}
};
That's all for today! Hope you had fun with this problem. More often than not, problems that you'll encounter in coding interviews or tests will just be variations of one of the classic problems. All you need to do is identify the variation and make some small tweaks in the original solution to solve the variation.
Come back tomorrow for another dose of Leetcode!
Cheers!