Java solution


  • 0
    L
       public int[] constructRectangle(int area) {
            if(area==0) {
                return new int[] {0,0};
            }
            int l=Integer.MAX_VALUE;
            int w=0;
            for(int i=1;i<=(int)Math.sqrt(area);i++) {
                int width = i;
                int length = area/width;
                if(area%width!=0) {
                    continue;
                }
                if((l-w)>(length-width)) {
                    l = length;
                    w = width;
                }            
            }
            
            return new int[]{l,w};
        }
    

Log in to reply
 

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