> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/algorithm-problems/daily-temperature.md).

# Daily Temperature

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]`.

{% hint style="info" %}
**Note:** The length of `temperatures` will be in the range `[1, 30000]`. Each temperature will be an integer in the range `[30, 100]`.
{% endhint %}

자료구조 : Stack이용

알고리즘\
스택 활용하기 테크닉🌟\
2-1. 일단 **인덱스값을 스택에 다 넣는다.** 처음에는 스택이 비어있고, result\[i] = 0.\
&#x20;\- stack.push(i), (i는 T.length-1부터 감소하는 반복문)\
2-2. 원소값을 비교한다.\
2-3. **판단기준**에 부합한다면 stack.peek()-i를 결과값에 넣는다.\
2-4. 부합하지 않는다면 기준에 부합하는 것을 찾을 때까지 stack.pop()한다. stack이 빌 때까지 못찾으면 결과값은 0. => **stack.isEmpty() ? 0 : stack.peek()-i**\
**=>2-1과 2-4에서 공통된 부분 : 스택이 비어있으면 result\[i] = 0**

**알고리즘을 Java로 구현**

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

\
&#x20;   <br>
