[LeetCode-Eazy] Arranging Coins
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
Given n, find the total number of full staircase rows that can be formed.
n is a non-negative integer and fits within the range of a 32-bit signed integer.
Example 1:
n = 5
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2.
Example 2:
n = 8
The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
Because the 4th row is incomplete, we return 3.
第一直覺想到 fibonacci 但不知道要怎樣處理就算了
暴力解去算每層每層到底需要幾個
然後扣掉可以用的硬幣
如果可以用的硬幣少於這一層要用的硬幣
就 return 剩下的硬幣數量
Golang
func arrangeCoins(n int) int {
if n == 0 {
return 0
}
cur := 1
rem := n - 1
for (rem >= cur + 1) {
cur+=1
rem -= cur
}
return cur
}