Problem
Given an array of integers temperatures
represents the daily temperatures, return an array answer
such that answer[i]
is the number of days you have to wait after the ith
day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0
instead.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Solution
Method 1 - Brute Force
- For each index
i
, iterate through the array to find the first indexj
whereT[j] > T[i]
or reach the array’s end. - If such a
j
is found, setans[i] = j - i
. - If no such
j
exists, setans[i] = 0
.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
- 🧺 Space complexity:
O(1)
Method 2 - Using Monotonic Stack
We can maintain the stack in Monotonic decreasing order.
We start from beginning of the array, and
- push element on stack if it is smaller than previous pushed element on stack
- pop elements from stack if current element is larger than element on stack
- A decreasing monotonic will always maintain decreasing order of elements when considered from bottom-to-top.
The algorithm can be summarized as follows:
- Initialize an empty stack
s
and an arrayans
of sizeN
(all initialized to 0). - Iterate through the temperatures array
temperatures
. - For each day
curr
, check if the current temperaturetemperatures[curr]
is higher than the temperatures corresponding to the indices in the stack (temperatures[stk.top()]
). If so, assign the answer for these indices and pop them from the stack. - Push the current day
curr
onto the stack to find the next warmer day forcurr
. - Any indices remaining in the stack at the end do not have a warmer future day, and their
ans
values remain 0, which is already initialized.
Note: We push the indices onto the stack to maintain the correct output format.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)
Method 3 - Monotonic Stack Approach 2
Another intuitive way to approach the problem using a monotonic stack is by starting from the end of the array. This method maintains a monotonic stack similar to the previous approach, but iterates from the last element to the first.
The algorithm can be summarized as follows:
- Initialize an empty stack
stk
and anans
array of sizeN
, all initialized to 0. - Iterate over
temperatures
from the end. - For each current day
curr
, pop elements from the stack that have temperatures(temperatures[stk.top()]
) less than or equal to current or today’s temperaturetemperatures[curr]
. These elements are popped as they are cooler and occur later thancurr
, thus they cannot be future answers for earlier days. - After popping, the stack is either empty or has a warmer temperature than
curr
.- If the stack is empty, set
ans[curr] = 0
as no warmer day exists. - Otherwise, set
ans[curr] = stk.top() - curr
, which is the difference between the indices of the next warmer day andcurr
.
- If the stack is empty, set
- Then, push
curr
onto the stack as it may be the next closest warmer day for future days. - Finally, return the
ans
.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)