492. Construct the Rectangle

Solution:

思路:
Time Complexity: O(1) Space Complexity: O(1)

Solution Code:

The W is always less than or equal to the square root of area
so we start searching at sqrt(area) till we find the result

public int[] constructRectangle(int area) {
        int w = (int)Math.sqrt(area);
    while (area%w!=0) w--;
    return new int[]{area/w, w};
}

你可能感兴趣的:(492. Construct the Rectangle)