> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/algorithm/739.-daily-temperatures.md).

# 739. Daily Temperatures

mono increasing/decreasing stack

{% hint style="info" %}
Leetcode 496, 503, 739
{% endhint %}

```java
class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] result = new int[temperatures.length];
        // monoincreasing stack
        Stack<Integer> stack = new Stack<>();
        
        for(int i=temperatures.length-1; i>=0; i--){
            // use while not if!
            while (!stack.isEmpty() && temperatures[stack.peek()]<=temperatures[i]){
                stack.pop();
            }
            result[i] = stack.isEmpty() ? 0 : stack.peek() - i;
            stack.push(i);
        }
        return result;
    }
}
```
