15 Jun 2024C++ / Python / TypeScriptEasy

Construct the Rectangle

Collected C++, Python, TypeScript solutions for construct the rectangle. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Construct the Rectangle. the solution files are available below.

solution files

  • C++ construct-the-rectangle/synced-solution.cpp
  • Python construct-the-rectangle/synced-solution.py
  • TypeScript construct-the-rectangle/synced-solution.ts

Solution files

Pythonconstruct-the-rectangle/synced-solution.py
def constructRectangle(area):
    w = int(area ** class="syntax-number">0.5)

    while area % w != class="syntax-number">0:
        w -= class="syntax-number">1

    return [area class=class="syntax-string">"syntax-comment">// w, w]
C++construct-the-rectangle/synced-solution.cpp
class Solution {
public:
    vector<int> constructRectangle(int area) {
        int w = (int)sqrt(area);

        while (area % w != class="syntax-number">0) {
            w--;
        }

        return {area / w, w};
    }
};
TypeScriptconstruct-the-rectangle/synced-solution.ts
function constructRectangle(area: number): number[] {
    let w = Math.floor(Math.sqrt(area));

    while (area % w !== class="syntax-number">0) {
        w--;
    }

    return [area / w, w];
}