# \#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`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up