Given a list of daily temperatures T
, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0
instead.
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73]
, your output should be [1, 1, 4, 2, 1, 1, 0, 0]
.
Note:
The length of temperatures
will be in the range [1, 30000]
.
Each temperature will be an integer in the range [30, 100]
.
class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
n = len(T)
res = [0 for _ in range(n)]
s = []
for i in range(n):
while s and T[s[-1]] < T[i]:
j = s.pop()
res[j] = i - j
s.append(i)
return res
class Solution {
public int[] dailyTemperatures(int[] T) {
int n = T.length;
int[] res = new int[n];
Deque<Integer> s = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
while (!s.isEmpty() && T[s.peek()] < T[i]) {
int j = s.pop();
res[j] = i - j;
}
s.push(i);
}
return res;
}
}
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
int n = T.size();
vector<int> ans(n);
stack<int> s;
for(int i = 0; i < n; ++i) {
while(!s.empty() && T[s.top()] < T[i]) {
int pre = s.top();
s.pop();
ans[pre] = i - pre;
}
s.push(i);
}
return ans;
}
};
func dailyTemperatures(T []int) []int {
n := len(T)
res := make([]int, n)
stack := make([]int, 0)
for i, v := range T {
for len(stack) != 0 && T[stack[len(stack)-1]] < v {
j := stack[len(stack)-1]
stack = stack[:len(stack)-1]
res[j] = i - j
}
stack = append(stack, i)
}
return res
}