leetcode (Arranging Coins)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85400122

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);

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85400122