Skip to content

Commit 1128337

Browse files
committed
Construct the rectangle.
1 parent 30c0c30 commit 1128337

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
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:
3+
4+
The area of the rectangular web page you designed must equal to the given target area.
5+
The width W should not be larger than the length L, which means L >= W.
6+
The difference between length L and width W should be as small as possible.
7+
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
8+
9+
 
10+
11+
Example 1:
12+
Input: area = 4
13+
Output: [2,2]
14+
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
15+
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.
16+
17+
Example 2:
18+
Input: area = 37
19+
Output: [37,1]
20+
21+
Example 3:
22+
Input: area = 122122
23+
Output: [427,286]
24+
 
25+
26+
Constraints:
27+
- 1 <= area <= 107
28+
*/
29+
class Solution {
30+
func constructRectangle(_ area: Int) -> [Int] {
31+
var width = Int(Double(area).squareRoot())
32+
while area % width != 0 {
33+
width -= 1
34+
}
35+
return [area / width, width]
36+
}
37+
}
38+
39+
let s = Solution()
40+
let r = s.constructRectangle(122122)
41+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/492.Construct the Rectangle.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
93. [Number Complement](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/476.Number%20Complement.playground/Contents.swift)
9898
94. [License Key Formatting](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/482.License%20Key%20Formatting.playground/Contents.swift)
9999
95. [Max Consecutive Ones](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/485.Max%20Consecutive%20Ones.playground/Contents.swift)
100+
96. [Construct the Rectangle](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/492.Construct%20the%20Rectangle.playground/Contents.swift)
100101

101102
#### Medium
102103

0 commit comments

Comments
 (0)