Show problems as
Classic problem #441EasyMathBinary Search

Arranging Coins

Real-world version: Complete Tiers Of The Tin Display
Tracked in this browser only
Read Arranging Coins on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A shop builds a stepped display of tins. Tier 1 across the top holds one tin, tier 2 below it holds two tins, and in general tier i holds i tins.

With n tins in stock, the staff work downwards and fill each tier completely before starting the next one, stopping when the stock runs short of what the next tier needs. Return how many tiers end up complete.

Examples

Example 1

Input
n = 7
Output
3

Tier 1 takes 1 tin, tier 2 takes 2 and tier 3 takes 3, which uses 6 of the 7 tins. Tier 4 needs 4 tins and only 1 is left, so 3 tiers are complete.

Example 2

Input
n = 999999999
Output
44720

The first 44720 tiers take 999961560 tins altogether, leaving 38439 tins. Tier 44721 needs 44721 tins, which is more than what is left, so 44720 tiers are complete.

Example 3

Input
n = 6
Output
3

Tiers 1, 2 and 3 take 1, 2 and 3 tins, using the stock exactly, so all 3 tiers are complete and nothing is left over.

Constraints

  • 1 <= n <= 2^31 - 1

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def arrange_coins(n: int) -> int:
Java
public int arrangeCoins(int n)