Accepted Java Solution using simple FOR loop(log n)


  • 0
    L

    Run Time = log n.
    Example,
    when n = 8, the for loop will run for 3 times which is nothing but log(base 2) 8 = 3.

        public int arrangeCoins(int n) {
            int ways = 0;
            for(int i = 1; i <= n; i++){
                n = n - i;
                ways++;
            }
            return ways;
        }
    

Log in to reply
 

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