Java Solution - O(n)


  • 0
    V

    From [1, sqrt(area)] find the largest number that is a factor of area

       public int[] constructRectangle(int area) {
            int limit = (int)Math.sqrt(area), width = 1, length = 1;
            for(int i = 1; i <= limit; i++) {
                if(area % i == 0) {
                    width = i;
                    length = area/i;
                }
            }
            return new int[]{length, width};
        }
    

Log in to reply
 

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