C++ 1-Line Simple Quadratic Equation Solution


  • 0
    M

    We need to solve integer inequality k * (k + 1) / 2 <= n < (k + 1) * (k + 2) / 2, thus we need to find integer solution of k^2 + k - 2 * n == 0 which has positive root int((sqrt(8.0 * n + 1) - 1) / 2).

    class Solution {
    public:
        int arrangeCoins(int n) {
            return (sqrt(8.0 * n + 1) - 1) / 2;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.