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

# Arranging Coins Python Solution with Tests

> Tested Python solution for LeetCode 441 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 441, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/arranging-coins/description/).

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

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

## Problem

You have `n` coins and you want to build a staircase with these coins. The staircase consists of `k` rows where the `i^th` row has exactly `i` coins. The last row of the staircase **may be** incomplete.

Given the integer `n`, return *the number of **complete rows** of the staircase you will build*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/04/09/arrangecoins1-grid.jpg)

```
Input: n = 5
Output: 2
Explanation: Because the 3rd row is incomplete, we return 2.
```

![Example 2](https://assets.leetcode.com/uploads/2021/04/09/arrangecoins2-grid.jpg)

```
Input: n = 8
Output: 3
Explanation: Because the 4th row is incomplete, we return 3.
```

### Constraints

* `1 <= n <= 2^31 - 1`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import isqrt


class Solution:
    # Time: O(1)
    # Space: O(1)
    def arrange_coins(self, n: int) -> int:
        # largest k with k * (k + 1) / 2 <= n
        return (isqrt(8 * n + 1) - 1) // 2
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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