Java Solution


  • 0
    S
        public int[] constructRectangle(int area) {
            int diff=0,prevdiff=1000000000;
            int[] lenwid = new int[2];
            if(area ==1){
                lenwid[0]=1;
                lenwid[1]=1;
                return lenwid;
            }
            for (int i=1; i<=Math.sqrt(area);i++){
                diff = (area/i)-i;
                if(diff<prevdiff && (Math.floor(area/i))*i==area){
                    prevdiff= diff;
                    lenwid[0] = area/i;
                    lenwid[1] =i;
                }
            }
            return lenwid;
        }
    }

Log in to reply
 

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