C# O(1) Solution if you don't know quadratic equation


  • 0
    C
        public int ArrangeCoins(int n) {
            if (n <= 0) { return 0; };
            if (n == 1) { return 1; };
            
            uint nAsuint = (uint)n;
            uint doubleN = (uint)(nAsuint * 2);
            uint upperNumber = (uint)Math.Sqrt(doubleN);
            
            while (upperNumber > 1)
            {
                if ((upperNumber * (upperNumber + 1) / 2) <= nAsuint){
                    break;
                }
                upperNumber--;
            }
            return (int)(upperNumber);
        }
    

Log in to reply
 

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