Skip to content

Commit 239a52b

Browse files
committedJan 20, 2023
Add solution #492
1 parent 82893aa commit 239a52b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|
162162
476|[Number Complement](./0476-number-complement.js)|Easy|
163163
491|[Non-decreasing Subsequences](./0491-non-decreasing-subsequences.js)|Medium|
164+
492|[Construct the Rectangle](./0492-construct-the-rectangle.js)|Easy|
164165
500|[Keyboard Row](./0500-keyboard-row.js)|Easy|
165166
506|[Relative Ranks](./0506-relative-ranks.js)|Easy|
166167
509|[Fibonacci Number](./0509-fibonacci-number.js)|Easy|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 492. Construct the Rectangle
3+
* https://leetcode.com/problems/construct-the-rectangle/
4+
* Difficulty: Easy
5+
*
6+
* A web developer needs to know how to design a web page's size. So, given
7+
* a specific rectangular web page’s area, your job by now is to design a
8+
* rectangular web page, whose length L and width W satisfy the following
9+
* requirements:
10+
*
11+
* The area of the rectangular web page you designed must equal to the given
12+
* target area. The width W should not be larger than the length L, which
13+
* means L >= W. The difference between length L and width W should be as
14+
* small as possible.
15+
*
16+
* Return an array [L, W] where L and W are the length and width of the web
17+
* page you designed in sequence.
18+
*/
19+
20+
/**
21+
* @param {number} area
22+
* @return {number[]}
23+
*/
24+
var constructRectangle = function(area) {
25+
let width = Math.floor(Math.sqrt(area));
26+
while (area % width) {
27+
width--;
28+
}
29+
return [area / width, width];
30+
};

0 commit comments

Comments
 (0)
Please sign in to comment.