1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// @Title: 排列硬币 (Arranging Coins)
// @Author: 15816537946@163.com
// @Date: 2020-12-09 09:31:09
// @Runtime: 4 ms
// @Memory: 2.2 MB
func arrangeCoins(n int) int {
	l, r := 0, n
	for l <= r {
		mid := (l + r) >> 1
		if (mid+1)*mid/2 > n {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	return r
}

/*
根据等差数列 an=n 的前 n 项和公式,列二元一次方程:x(x+1)/2=n,直接解 x
func arrangeCoins(n int) int {
	return int(math.Floor(math.Sqrt(float64(8*n+1)))-1) / 2
}
*/