Daily Temperatures

MediumTopicsCompany TagsHints

You are given an array of integers temperatures where temperatures[i] represents the daily temperatures on the ith day.

Return an array result where result[i] is the number of days after the ith day before a warmer temperature appears on a future day. If there is no day in the future where a warmer temperature will appear for the ith day, set result[i] to 0 instead.

Example 1:

Input: temperatures = [30,38,30,36,35,40,28]
 
Output: [1,4,1,2,1,0,0]

Example 2:

Input: temperatures = [22,21,20]
 
Output: [0,0,0]

Constraints:

  • 1 <= temperatures.length <= 100,000.
  • 1 <= temperatures[i] <= 100

Solution:

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) 
    {
        std::stack<int> myStack;
        std::vector<int> returnVec(temperatures.size());
 
        for(int index = 0; index < temperatures.size(); index++)
        {
            //follow the monotonic stack methodology where you keep on popping to keep
            //the stack in sorted order
            while(!myStack.empty() && temperatures[index] > temperatures[myStack.top()])
            {
                //save the popped index
                int poppedIndex = myStack.top();
                myStack.pop();
 
                //take the current index minus the popped index and then add the result 
                //at the popped index to the returnVec
                int difference = index - poppedIndex;
                returnVec[poppedIndex] = difference;
            }
 
            //push the larger value onto the stack
            myStack.push(index);
        }
 
        return returnVec;
    }
};
 

Explanation:

This one was pretty fun all things considered, this is another monotonic stack solution, you basically keep your stack in increasing order by popping off values that are lesser than the current element right. The goal is to keep track of how many days until a hotter day.

The solution here was to not actually push the temperatures themselves onto the stack, because while you could compare them correctly and outright, you have no real way of counting how far away in between values would be right. Instead the solution is much more nuanced, you instead store the index of each day.

By storing the index you have the relative position of each day on the stack, as well as a way to find their value because you can simply take the index off the stack and use it to check the value at that index in the temperature array.

So for the solution, you push an index onto the stack, and then if the next value is smaller than the one on top of the stack, you push the index of that value onto the stack. The now monotonic part comes from when you have an element that is larger than the top of the stack, when this happens, what you do is you take the current element at the top of the stack, so the index for that element, and then you subtract it from the current element because you finally found a day that was hotter than it. So at that index you then push the difference onto the return vector, and you do this all the way down the line, ending up with an O(n) solution with at maximum O(n) space.