C++_9ms_Accepted


  • 1
    class Solution {
    public:
    int arrangeCoins(int n) {
        if(n <= 1) return n;
        long left = 0, right = n;
        while(left <= right){
            long mid = left + (right - left)/2;
            long sum = ((1 + mid) * mid)/2;
            if(sum <= n){left = mid + 1;}
            else {right = mid - 1;}
        }
        return left - 1;
    }
    };

  • 0
    Z

    @jasonshieh said in C++_9ms_Accepted:

    class Solution {
    public:
    int arrangeCoins(int n) {
    if(n <= 1) return n;
    long left = 0, right = n;
    while(left <= right){
    long mid = left + (right - left)/2;
    long sum = ((1 + mid) * mid)/2;
    if(sum <= n){left = mid + 1;}
    else {right = mid - 1;}
    }
    return left - 1;
    }
    };


Log in to reply
 

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