> ## 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.

# Water Bottles Python Solution with Tests

> Tested Python solution for LeetCode 1518 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1518, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/water-bottles/description/).

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

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

## Problem

There are `numBottles` water bottles that are initially full of water. You can exchange `numExchange` empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers `numBottles` and `numExchange`, return *the **maximum** number of water bottles you can drink*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png)

```
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.
```

![Example 2](https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png)

```
Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 15 + 3 + 1 = 19.
```

### Constraints

* `1 <= numBottles <= 100`
* `2 <= numExchange <= 100`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log numBottles)
    # Space: O(1)
    def num_water_bottles(self, num_bottles: int, num_exchange: int) -> int:
        drunk = num_bottles
        empty = num_bottles
        while empty >= num_exchange:
            full, empty = divmod(empty, num_exchange)
            drunk += full
            empty += full
        return drunk
```

## Complexity

| Time | Space |
| - | - |
| O(log numBottles) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.