Arranging Coins
Find the maximum number of complete staircase rows that can be built with n coins using binary search on the answer with upward rounding.
01The 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.
| Input | Output | Why |
|---|---|---|
n = 5 | 2 | Rows need 1 + 2 = 3 coins. 2 coins left cannot finish row 3 |
n = 8 | 3 | Rows need 1 + 2 + 3 = 6 coins. 2 coins left cannot finish row 4 |
n = 1 | 1 | 1 coin completes row 1 |
n = 3 | 2 | 1 + 2 = 3 coins exactly completes row 2 |
Constraints: 1 ≤ n ≤ 2³¹ − 1.
Loading the rest of the lesson…