Open Tcdian opened 4 years ago
/**
* @param {number} n
* @return {number}
*/
var arrangeCoins = function(n) {
let left = 0;
let right = n;
while (left <= right) {
const mid = (left + right) >> 1;
const count = (1 + mid) * mid / 2;
if (count <= n && count + mid + 1 > n) {
return mid;
} else if (count > n) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
};
function arrangeCoins(n: number): number {
let left = 0;
let right = n;
while (left <= right) {
const mid = (left + right) >> 1;
const count = (1 + mid) * mid / 2;
if (count <= n && count + mid + 1 > n) {
return mid;
} else if (count > n) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
};
441. Arranging Coins
你总共有
n
枚硬币,你需要将它们摆成一个阶梯形状,第k
行就必须正好有k
枚硬币。给定一个数字
n
,找出可形成完整阶梯行的总行数。n
是一个非负整数,并且在32位有符号整型的范围内。Example 1
Example 2