[JAVA] Cleaner and easier to understand solution.


  • 4
    S
        public int arrangeCoins(int n) {
            if(n < 1)
                return 0;
            for(int i = 1; ;i++){
                n -=i;
                if(n < 0)
                    return i-1;
            }
        }
    }
    

    As coins increase by 1 in every row beginning from 1. keep on subtracting same number of coins from the 'n' until it goes negative. When it goes negative, current iteration 'i' is half filled. So, definitely previous row will be fully filled.


  • 1
    D

    Excellent solution..Thank you!


Log in to reply
 

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