Length to increase, width to decrease; starting at sqrt area; Java


  • 0
    V
        public int[] constructRectangle(int area) 
        {
            int w = (int)Math.sqrt(area), l = w;
            int[] toRet = new int[2];
            while(w >= 1)
            {
                if(l * w == area)
                    break;
                if(l * w < area)
                    l++;
                else
                    w--;
            }
            toRet[0] = l;
            toRet[1] = w;
            return toRet;
        }

Log in to reply
 

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