Skip to content

Commit b3225a0

Browse files
halfrostdezhiy
authored andcommitted
Add solution 492
1 parent f09ea6c commit b3225a0

File tree

8 files changed

+222
-4
lines changed

8 files changed

+222
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135

136136
## 二. 目录
137137

138-
以下已经收录了 723 道题的题解,还有 11 道题在尝试优化到 beats 100%
138+
以下已经收录了 724 道题的题解,还有 11 道题在尝试优化到 beats 100%
139139

140140
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
141141
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
@@ -630,7 +630,7 @@
630630
|0489|Robot Room Cleaner||74.6%|Hard||
631631
|0490|The Maze||54.0%|Medium||
632632
|0491|Increasing Subsequences|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0491.Increasing-Subsequences)|49.6%|Medium||
633-
|0492|Construct the Rectangle||51.8%|Easy||
633+
|0492|Construct the Rectangle|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0492.Construct-the-Rectangle)|51.8%|Easy||
634634
|0493|Reverse Pairs|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0493.Reverse-Pairs)|28.8%|Hard||
635635
|0494|Target Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0494.Target-Sum)|45.3%|Medium||
636636
|0495|Teemo Attacking||56.5%|Easy||
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
import "math"
4+
5+
func constructRectangle(area int) []int {
6+
ans := make([]int, 2)
7+
W := int(math.Sqrt(float64(area)))
8+
for W >= 1 {
9+
if area%W == 0 {
10+
ans[0], ans[1] = area/W, W
11+
break
12+
}
13+
W -= 1
14+
}
15+
return ans
16+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question492 struct {
9+
para492
10+
ans492
11+
}
12+
13+
// area 是参数
14+
type para492 struct {
15+
area int
16+
}
17+
18+
// ans 是答案
19+
type ans492 struct {
20+
ans []int
21+
}
22+
23+
func Test_Problem492(t *testing.T) {
24+
25+
qs := []question492{
26+
27+
{
28+
para492{4},
29+
ans492{[]int{2, 2}},
30+
},
31+
32+
{
33+
para492{37},
34+
ans492{[]int{37, 1}},
35+
},
36+
37+
{
38+
para492{122122},
39+
ans492{[]int{427, 286}},
40+
},
41+
}
42+
43+
fmt.Printf("------------------------Leetcode Problem 492------------------------\n")
44+
45+
for _, q := range qs {
46+
_, p := q.ans492, q.para492
47+
fmt.Printf("【input】:%v 【output】:%v\n", p, constructRectangle(p.area))
48+
}
49+
fmt.Printf("\n\n\n")
50+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [492. Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)
2+
3+
4+
## 题目
5+
6+
A web developer needs to know how to design a web page's size.
7+
So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page,
8+
whose length L and width W satisfy the following requirements:
9+
10+
The area of the rectangular web page you designed must equal to the given target area.
11+
The width W should not be larger than the length L, which means L >= W.
12+
The difference between length L and width W should be as small as possible.
13+
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
14+
15+
**Example 1:**
16+
17+
Input: area = 4
18+
Output: [2,2]
19+
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
20+
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.
21+
22+
**Example 2:**
23+
24+
Input: area = 37
25+
Output: [37,1]
26+
27+
**Example 3:**
28+
29+
Input: area = 122122
30+
Output: [427,286]
31+
32+
**Constraints**
33+
34+
- 1 <= area <= 10000000
35+
36+
## 题目大意
37+
38+
作为一位 web 开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
39+
40+
1. 你设计的矩形页面必须等于给定的目标面积。
41+
2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 。
42+
3. 长度 L 和宽度 W 之间的差距应当尽可能小。
43+
44+
你需要按顺序输出你设计的页面的长度 L 和宽度 W。
45+
46+
## 解题思路
47+
48+
- 令 W 等于根号 area
49+
- 在 W 大于等于 1 的情况下,判断 area%W 是否等于 0,如果不相等 W 就减 1 继续循环,如果相等就返回 [area/W, W]
50+
51+
## 代码
52+
53+
```go
54+
55+
package leetcode
56+
57+
import "math"
58+
59+
func constructRectangle(area int) []int {
60+
ans := make([]int, 2)
61+
W := int(math.Sqrt(float64(area)))
62+
for W >= 1 {
63+
if area%W == 0 {
64+
ans[0], ans[1] = area/W, W
65+
break
66+
}
67+
W -= 1
68+
}
69+
return ans
70+
}
71+
72+
``

website/content/ChapterFour/0400~0499/0491.Increasing-Subsequences.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ func generateIncSubsets(nums []int, current int, c []int, res *[][]int) {
8787
----------------------------------------------
8888
<div style="display: flex;justify-content: space-between;align-items: center;">
8989
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0485.Max-Consecutive-Ones/">⬅️上一页</a></p>
90-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0493.Reverse-Pairs/">下一页➡️</a></p>
90+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0492.Construct-the-Rectangle/">下一页➡️</a></p>
9191
</div>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [492. Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)
2+
3+
4+
## 题目
5+
6+
A web developer needs to know how to design a web page's size.
7+
So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page,
8+
whose length L and width W satisfy the following requirements:
9+
10+
The area of the rectangular web page you designed must equal to the given target area.
11+
The width W should not be larger than the length L, which means L >= W.
12+
The difference between length L and width W should be as small as possible.
13+
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
14+
15+
**Example 1:**
16+
17+
Input: area = 4
18+
Output: [2,2]
19+
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
20+
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.
21+
22+
**Example 2:**
23+
24+
Input: area = 37
25+
Output: [37,1]
26+
27+
**Example 3:**
28+
29+
Input: area = 122122
30+
Output: [427,286]
31+
32+
**Constraints**
33+
34+
- 1 <= area <= 10000000
35+
36+
## 题目大意
37+
38+
作为一位 web 开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
39+
40+
1. 你设计的矩形页面必须等于给定的目标面积。
41+
2. 宽度 W 不应大于长度 L,换言之,要求 L >= W 。
42+
3. 长度 L 和宽度 W 之间的差距应当尽可能小。
43+
44+
你需要按顺序输出你设计的页面的长度 L 和宽度 W。
45+
46+
## 解题思路
47+
48+
- 令 W 等于根号 area
49+
- 在 W 大于等于 1 的情况下,判断 area%W 是否等于 0,如果不相等 W 就减 1 继续循环,如果相等就返回 [area/W, W]
50+
51+
## 代码
52+
53+
```go
54+
55+
package leetcode
56+
57+
import "math"
58+
59+
func constructRectangle(area int) []int {
60+
ans := make([]int, 2)
61+
W := int(math.Sqrt(float64(area)))
62+
for W >= 1 {
63+
if area%W == 0 {
64+
ans[0], ans[1] = area/W, W
65+
break
66+
}
67+
W -= 1
68+
}
69+
return ans
70+
}
71+
72+
``
73+
74+
75+
----------------------------------------------
76+
<div style="display: flex;justify-content: space-between;align-items: center;">
77+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0491.Increasing-Subsequences/">⬅️上一页</a></p>
78+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0493.Reverse-Pairs/">下一页➡️</a></p>
79+
</div>

website/content/ChapterFour/0400~0499/0493.Reverse-Pairs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,6 @@ func reversePairs2(nums []int) int {
158158

159159
----------------------------------------------
160160
<div style="display: flex;justify-content: space-between;align-items: center;">
161-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0491.Increasing-Subsequences/">⬅️上一页</a></p>
161+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0492.Construct-the-Rectangle/">⬅️上一页</a></p>
162162
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0494.Target-Sum/">下一页➡️</a></p>
163163
</div>

website/content/ChapterTwo/Math.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ weight: 12
6262
|0477|Total Hamming Distance|[Go]({{< relref "/ChapterFour/0400~0499/0477.Total-Hamming-Distance.md" >}})|Medium||||51.5%|
6363
|0478|Generate Random Point in a Circle|[Go]({{< relref "/ChapterFour/0400~0499/0478.Generate-Random-Point-in-a-Circle.md" >}})|Medium||||39.1%|
6464
|0483|Smallest Good Base|[Go]({{< relref "/ChapterFour/0400~0499/0483.Smallest-Good-Base.md" >}})|Hard||||37.3%|
65+
|0492|Construct the Rectangle|[Go]({{< relref "/ChapterFour/0400~0499/0492.Construct-the-Rectangle.md" >}})|Easy||||51.8%|
6566
|0497|Random Point in Non-overlapping Rectangles|[Go]({{< relref "/ChapterFour/0400~0499/0497.Random-Point-in-Non-overlapping-Rectangles.md" >}})|Medium||||39.1%|
6667
|0507|Perfect Number|[Go]({{< relref "/ChapterFour/0500~0599/0507.Perfect-Number.md" >}})|Easy||||37.2%|
6768
|0509|Fibonacci Number|[Go]({{< relref "/ChapterFour/0500~0599/0509.Fibonacci-Number.md" >}})|Easy||||67.8%|

0 commit comments

Comments
 (0)