{x}
blog image

Construct the Rectangle

A web developer needs to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:

  1. The area of the rectangular web page you designed must equal to the given target area.
  2. The width W should not be larger than the length L, which means L >= W.
  3. The difference between length L and width W should be as small as possible.

Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.

 

Example 1:

Input: area = 4
Output: [2,2]
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. 
But according to requirement 2, [1,4] is illegal; according to requirement 3,  [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.

Example 2:

Input: area = 37
Output: [37,1]

Example 3:

Input: area = 122122
Output: [427,286]

 

Constraints:

  • 1 <= area <= 107

Solution Explanation

The problem asks to find the dimensions (length L and width W) of a rectangle with a given area, such that L >= W and the difference between L and W is minimized.

The optimal solution involves finding the closest integer to the square root of the area. This integer will represent the width W. The length L is then simply the area divided by W.

Algorithm:

  1. Find the initial width: Calculate the square root of the area and convert it to an integer. This gives us an initial estimate for the width W.

  2. Adjust the width: If the area is not perfectly divisible by the initial W (meaning the initial W doesn't create a perfect rectangle), we need to decrement W until we find a divisor of the area. This ensures that we have a valid width and length that multiply to the target area.

  3. Calculate the length: Once a valid W is found, calculate the length L by dividing the area by W.

  4. Return the result: Return the length and width as an array [L, W].

Time Complexity Analysis:

The dominant operation is the while loop in the code. In the worst case, this loop iterates up to approximately √area times. Therefore, the time complexity is O(√area). This is a sub-linear time complexity because it grows slower than linearly with respect to the input area.

Space Complexity Analysis:

The space used by the algorithm is constant, regardless of the input area. We only store a few integer variables (area, w, l). Therefore, the space complexity is O(1).

Code Explanation (Python):

class Solution:
    def constructRectangle(self, area: int) -> List[int]:
        w = int(sqrt(area))  # Initial guess for width
        while area % w != 0:  # Adjust width until it's a divisor of area
            w -= 1
        return [area // w, w] # Return length and width

The Python code directly implements the algorithm. The int(math.sqrt(area)) line finds the integer part of the square root. The while loop iteratively reduces w until a perfect divisor is found. Then the length is calculated using integer division (//).

Code Explanation (Java):

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

The Java code is very similar to the Python code. It uses Math.sqrt() to find the square root, and a while loop to adjust the width.

Code Explanation (C++):

class Solution {
public:
    vector<int> constructRectangle(int area) {
        int w = sqrt(1.0 * area); //Note the use of 1.0 to ensure floating point calculation before casting.
        while (area % w != 0) --w;
        return {area / w, w};
    }
};

The C++ code is also similar but uses sqrt(1.0 * area) to ensure a floating point calculation before converting to an integer.

Code Explanation (Go):

func constructRectangle(area int) []int {
	w := int(math.Sqrt(float64(area)))
	for area%w != 0 {
		w--
	}
	return []int{area / w, w}
}

The Go code uses math.Sqrt() and a for loop to achieve the same functionality as the other languages.

All the provided solutions follow the same algorithmic approach and have the same time and space complexity. The choice of language only affects the syntax but not the underlying algorithm.