---
# System prepended metadata

title: \#441 Arranging Coins
tags: [leetcode, leetcode-easy]

---

# \#441 Arranging Coins
## *i階樓梯的每層由i枚硬幣組成, 回傳n枚硬幣所能組成的最多階樓梯(硬幣可能剩餘,最後一層必須滿足i枚硬幣組成)*
## Log
 - build 20210819 by syhuang

## 數學解法
 - 等差級數公式:((x+1)x)/2 <= n
 - leetcode submit runtime 96%, memory 57%
```javascript=
var arrangeCoins = function(n) {
    return Math.floor((Math.sqrt(8*n+1)-1)/2);
};
```
## 初見
 - 跑迴圈每層扣除, 直到扣到負數為止
 - leetcode submit runtime 50%, memory 75%
```javascript=
var arrangeCoins = function(n) {
    let leftCoins = n;
    let result = 1
    for(; leftCoins >= 0; result++) leftCoins-=result;
    return result-2;
};
```
## 備註
- 開根號運算的時間複雜度, 一般視為O(1)
## 參考
###### tags: `leetcode`, `leetcode-easy`