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:
T = [73, 74, 75, 71, 69, 72, 76, 73]
ans = [1, 1, 4, 2, 1, 1, 0, 0]
Nested loop: for each node traverse until a greater value is found
Time: O(n²)
Space: O(1)
Use a stack with a pair <val, idx>
.
- Count the nodes and create an answer array
- Add elements to the stack as long as those are smaller or equal to stack.peek()
- When a node value is greater than stack.peek(), pop elements as long as
curr.val > stack.peek()
. Setanswer[popped.idx] = curr.head
. I.e. the current element is bigger than x elements in the stack
Time: O(n)
Space: O(n)
type ListNode struct {
Val int
Next *ListNode
}
func dailyTemperatures(T []int) []int {
n := len(T)
if n == 0 {
return T
}
head := &ListNode{n-1, nil}
ans := make([]int, n)
for i := n - 1; i >= 0; i-- {
for head != nil && T[i] >= T[head.Val] {
head = head.Next
}
if head != nil {
ans[i] = head.Val - i
}
head = &ListNode{i, head}
}
return ans
}
class Solution
public int[] dailyTemperatures(int[] T) {
int n = T.length;
int[] ans = new int[n];
if(n == 0) { return ans; }
Stack<Integer> stack = new Stack();
for(int i = n - 1; i >= 0; i--) {
while(!stack.empty() && T[stack.peek()] <= T[i]) {
stack.pop();
}
if(!stack.empty()) {
ans[i] = stack.peek() - i;
}
stack.push(i);
}
return ans;
}
}