Problem
You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith 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:
$$ \begin{matrix} \colorbox{orange} \$ \\ \colorbox{orange} \$ & \colorbox{orange} \$ \\ \colorbox{orange} \$ & \colorbox{orange} \$ &\colorbox{white} 0 \end{matrix} $$
| |
Example 2:
$$ \begin{matrix} \colorbox{orange} \$ \\ \colorbox{orange} \$ & \colorbox{orange} \$ \\ \colorbox{orange} \$ & \colorbox{orange} \$ &\colorbox{orange} \$ \\ \colorbox{orange} \$ & \colorbox{orange} \$ & \colorbox{white} 0 & \colorbox{white} 0 \\ \end{matrix} $$
| |
Solution
Method 1 - Greedy
- For each row keep on subtracting the num elements in row, till n is positive and count the rows
Eg. n = 8; numCoinsInRow = 1; row = 1; n = 7 numCoinsInRow = 2; row = 2; n = 5; numCoinsInRow = 3; row = 3; n = 2 numCoinsInRow = 4, n = -2 so break; Answer = row = 3
Code
| |
Complexity
- ⏰ Time complexity:
O(sqrt (n))- We take larger and larger chunks with each iteration - 🧺 Space complexity:
O(1)
Method 2 - Using Arithematic Progression and Binary Search
Each row increment is following arithematic progression with a = 1, and d = 1. When we have 3 rows, we have 6 coins. When we have 4 rows, we have 10 coins. When we have N rows, we have N(N+1) / 2 coins.
We can implement Binary Search now, by setting
- Our “left” marker to 0
- Our “right” marker to N, since we cannot use more coins than we our given
- Our “pivot” to
left + (right-left)/2, we use this equation instead of(left + right)/2to avoid getting an integer overflow - The “area” of our proposed staircase to
(pivot * (pivot+1))/2
To implement Binary Search with these variables, we:
- Check if our area is equal to the amount of coins that we are given
- at which point we return “pivot”
- If we used more coins than our limit of coins, then we set “right” to “pivot-1”, to use less coins during the next iteration
- If we used less coins than our limit of coins, then we set “left” to “pivot+1”, to use more coins during the next iteration
- When our “left” is greater than our “right”, our “left” will be set to a incomplete row and our “right” will be set to a complete row, so we return “right”
Code
| |
Complexity
- ⏰ Time complexity:
O(log (n)) - 🧺 Space complexity:
O(1)
Method 3 - Using Arithmetic Progression and Algebra
So, now we have to so n coins and we have to find N.
| |
Code:
| |
Complexity
- ⏰ Time complexity:
O(log n) - 🧺 Space complexity:
O(1)