All problems
0639EasyMathBinary Search

Complete Tiers Of The Tin Display

Tracked in this browser only
Write code

Trains the technique from

LeetCode 441Arranging Coins

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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