Arranging Coins (LeetCode 441): Solution and Explanation
Build a staircase where row i holds i coins. Given n coins, return how many complete rows you can build.
Intuition
A staircase with k full rows holds 1 + 2 + ... + k = k(k+1)/2 coins. That number only grows as k grows, so the rows that fit form a prefix: every k up to some limit fits, and every k beyond it does not.
Whenever a yes/no question flips exactly once along a sorted range, binary search finds the flip point. Here the question is "does k(k+1)/2 <= n?", and the answer is the last k where it is still yes.
Searching k in [0, n] takes about 31 steps even for n = 2^31 - 1. The only trap is overflow: k(k+1) for k near 2^31 does not fit in a 32-bit integer, so the multiplication must be done in 64 bits.
Approach: Binary search on k
- Set
lo = 0andhi = n. - While
lo <= hi, takek = (lo + hi) // 2and computeneed = k * (k + 1) / 2in 64-bit arithmetic. - If
need == n, k rows use every coin exactly: return k. - If
need < n, k rows fit, so try more:lo = k + 1. - Otherwise there are not enough coins:
hi = k - 1. - When the loop ends,
hiis the largest k that fits. Return it.
Step-by-step example
n = 8
lo = 0,hi = 8.k = 4needs 10 coins, more than 8, sohi = 3.k = 1needs 1 coin, which fits, solo = 2.k = 2needs 3 coins, which fits, solo = 3.k = 3needs 6 coins, which fits, solo = 4.- Now
lo = 4 > hi = 3, so the loop stops and the answer ishi = 3.
Code: Python, Java, C++
1class Solution:
2 def arrangeCoins(self, n: int) -> int:
3 lo, hi = 0, n
4 while lo <= hi:
5 k = (lo + hi) // 2
6 need = k * (k + 1) // 2
7 if need == n:
8 return k
9 if need < n:
10 lo = k + 1
11 else:
12 hi = k - 1
13 return hi1class Solution {
2 public int arrangeCoins(int n) {
3 long lo = 0, hi = n;
4 while (lo <= hi) {
5 long k = lo + (hi - lo) / 2;
6 long need = k * (k + 1) / 2;
7 if (need == n) return (int) k;
8 if (need < n) {
9 lo = k + 1;
10 } else {
11 hi = k - 1;
12 }
13 }
14 return (int) hi;
15 }
16}1class Solution {
2public:
3 int arrangeCoins(int n) {
4 long long lo = 0, hi = n;
5 while (lo <= hi) {
6 long long k = lo + (hi - lo) / 2;
7 long long need = k * (k + 1) / 2;
8 if (need == n) return (int) k;
9 if (need < n) {
10 lo = k + 1;
11 } else {
12 hi = k - 1;
13 }
14 }
15 return (int) hi;
16 }
17};Time and space complexity
O(log n)
The search range [0, n] halves every iteration, so at most about 32 iterations run for 32-bit n.
O(1)
Only lo, hi, k and need are stored.
Other approaches
Closed form with the quadratic formula (time O(1), space O(1))
Solve k(k+1)/2 <= n for k: k = floor((sqrt(8n + 1) - 1) / 2). Use an integer square root (math.isqrt in Python) or check the result with one adjustment step, since floating-point sqrt can be off by one for large n.
Subtract row by row (time O(sqrt n), space O(1))
Remove 1, then 2, then 3 coins, counting rows until the next row cannot be paid for. It is simple but takes about sqrt(2n) steps, around 65,000 for the largest input.
Common mistakes
- Computing
k * (k + 1)in 32-bit integers. For k around 2^31 it overflows and the comparison becomes wrong. - Returning
loinstead ofhiafter the loop. When the loop ends, lo is one past the answer. - Using floating-point sqrt without a correction step. For n near 2^31, rounding can give an answer one too large or too small.
Frequently asked questions
Why does the search start with hi = n instead of something smaller?
n is a safe upper bound since k rows need at least k coins. A tighter bound such as sqrt(2n) + 1 works too and saves a few iterations, but the gain is tiny.
Is the closed form better in an interview?
It is O(1) and short, but you need to explain the floating-point risk. Many interviewers prefer the binary search because it shows the monotonic-predicate pattern and handles overflow explicitly.
What if n is exactly a triangular number?
Then some k has k(k+1)/2 == n and every coin is used. The early return handles it, and without it the loop would still end with hi = k.