739 Daily Temperatures
739. Daily Temperatures
1. Question
2. Implementation
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] res = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
int curTemp = temperatures[i];
while (!stack.isEmpty() && curTemp > temperatures[stack.peek()]) {
res[stack.peek()] = i - stack.peek();
stack.pop();
}
stack.push(i);
}
return res;
}
}3. Time & Space Complexity
Last updated