LeetCode Solutions

492. Construct the Rectangle

Time: $O(n)$

Space: $O(1)$

			

class Solution {
 public:
  vector<int> constructRectangle(int area) {
    int width = sqrt(area);

    while (area % width)
      --width;

    return {area / width, width};
  }
};
			

class Solution {
  public int[] constructRectangle(int area) {
    int width = (int) Math.sqrt(area);

    while (area % width > 0)
      --width;

    return new int[] {area / width, width};
  }
}