algorithm notes: leetcode#492 construct the rectangle

Problem


Solution


Initial thoughts

Python implementation

1
2
3
4
5
6
7
8
9
10
class :
def constructRectangle(self, area):
"""
:type area: int
:rtype: List[int]
"""
width = int(area**0.5)
while area % width:
width -= 1;
return area//width, width

Java implementation

1
2
3
4
5
6
7
8
9
10
11
class {
public int[] constructRectangle(int area) {
int[] ans = new int[2];
ans[1] = (int)Math.sqrt(area);
while(area%ans[1]!=0){
ans[1]--;
}
ans[0] = area / ans[1];
return ans;
}
}

Time complexity

O(n).

Space complexity

O(1).


492. Construct the Rectangle
(中文版) 算法笔记: 力扣#492 构造矩形