> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Construct the Rectangle Python Solution

> Tested Python solution for LeetCode 492 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 492, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/construct-the-rectangle/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 492   # by problem number
lcpy gen -s construct_the_rectangle   # by problem name
```

## Problem

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.

### Examples

```
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.
```

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

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

### Constraints

* 1 \<= area \<= 10^7

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_the_rectangle/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_the_rectangle/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import math


class Solution:
    # Time: O(sqrt(area))
    # Space: O(1)
    def construct_rectangle(self, area: int) -> list[int]:
        width = math.isqrt(area)
        while area % width != 0:
            width -= 1
        return [area // width, width]
```

## Complexity

| Time | Space |
| - | - |
| O(sqrt(area)) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.