Title:Arranging Coins 441
Difficulty:Easy
原题leetcode地址: https://leetcode.com/problems/arranging-coins/
1. 数学公式,i注意溢出问题
时间复杂度:O(1)。
空间复杂度:O(1)。
/**
* 数学公式: x(x+1)/2<=n --> x<=(sqrt(8*n+1)-1)/2,注意溢出问题
* @param n
* @return
*/
public static int arrangeCoins(int n) {
return (int) ((Math.sqrt(8 * (long) n + 1) - 1) / 2);
}