> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Daily Temperatures Python Solution with Tests

> Tested Python solution for LeetCode 739 with 35 pytest cases. Generate a practice environment with lcpy.

LeetCode 739, Medium. Topics: Array, Stack, Monotonic Stack. [View on LeetCode](https://leetcode.com/problems/daily-temperatures/description/).

Generate this problem as a practice environment: tested reference solution, 35 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 739   # by problem number
lcpy gen -s daily_temperatures   # by problem name
```

## Problem

Given an array of integers `temperatures` represents the daily temperatures, return an array `answer` such that `answer[i]` is the number of days you have to wait after the `ith` day to get a warmer temperature. If there is no future day for which this is possible, keep `answer[i] == 0` instead.

### Examples

```
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
```

**Explanation:**

* For input `[73,74,75,71,69,72,76,73]`, the output should be `[1,1,4,2,1,1,0,0]`.
* For example, the first temperature is 73. The next warmer temperature is 74, which is 1 day later, so we put 1.
* The second temperature is 74. The next warmer temperature is 75, which is 1 day later, so we put 1.
* The third temperature is 75. The next warmer temperature is 76, which is 4 days later, so we put 4.

```
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
```

```
Input: temperatures = [30,60,90]
Output: [1,1,0]
```

### Constraints

* `1 <= temperatures.length <= 10^5`
* `30 <= temperatures[i] <= 100`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/daily_temperatures/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/daily_temperatures/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def daily_temperatures(self, temperatures: list[int]) -> list[int]:
        result = [0] * len(temperatures)
        stack: list[int] = []

        for i, temp in enumerate(temperatures):
            while stack and temperatures[stack[-1]] < temp:
                prev_index = stack.pop()
                result[prev_index] = i - prev_index
            stack.append(i)

        return result
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(n)  |

## Tags

[Grind](/catalog/grind), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
