441. 排列硬币

题目:

思路:

【1】无

代码展示:

//时间1 ms 击败 100%
//内存38.4 MB 击败 90.52%
class Solution {
    public int arrangeCoins(int n) {
        int left = 1, right = n;
        while (left < right) {
            int mid = (right - left + 1) / 2 + left;
            if ((long) mid * (mid + 1) <= (long) 2 * n) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }
}
//时间1 ms 击败 100%
//内存38.6 MB 击败 76.89%
class Solution {
    public int arrangeCoins(int n) {
        return (int) ((Math.sqrt((long) 8 * n + 1) - 1) / 2);
    }
}

 

posted @ 2023-08-17 11:58  忧愁的chafry  阅读(5)  评论(0编辑  收藏  举报