Add solution 492

This commit is contained in:
halfrost
2021-11-06 19:18:25 -07:00
committed by halfrost
parent f09ea6cd09
commit b3225a0f11
8 changed files with 222 additions and 4 deletions

View File

@ -135,7 +135,7 @@
## 二. 目录 ## 二. 目录
以下已经收录了 723 道题的题解,还有 11 道题在尝试优化到 beats 100% 以下已经收录了 724 道题的题解,还有 11 道题在尝试优化到 beats 100%
| No. | Title | Solution | Acceptance | Difficulty | Frequency | | No. | Title | Solution | Acceptance | Difficulty | Frequency |
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:| |:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
@ -630,7 +630,7 @@
|0489|Robot Room Cleaner||74.6%|Hard|| |0489|Robot Room Cleaner||74.6%|Hard||
|0490|The Maze||54.0%|Medium|| |0490|The Maze||54.0%|Medium||
|0491|Increasing Subsequences|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0491.Increasing-Subsequences)|49.6%|Medium|| |0491|Increasing Subsequences|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0491.Increasing-Subsequences)|49.6%|Medium||
|0492|Construct the Rectangle||51.8%|Easy|| |0492|Construct the Rectangle|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0492.Construct-the-Rectangle)|51.8%|Easy||
|0493|Reverse Pairs|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0493.Reverse-Pairs)|28.8%|Hard|| |0493|Reverse Pairs|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0493.Reverse-Pairs)|28.8%|Hard||
|0494|Target Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0494.Target-Sum)|45.3%|Medium|| |0494|Target Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0494.Target-Sum)|45.3%|Medium||
|0495|Teemo Attacking||56.5%|Easy|| |0495|Teemo Attacking||56.5%|Easy||

View File

@ -0,0 +1,16 @@
package leetcode
import "math"
func constructRectangle(area int) []int {
ans := make([]int, 2)
W := int(math.Sqrt(float64(area)))
for W >= 1 {
if area%W == 0 {
ans[0], ans[1] = area/W, W
break
}
W -= 1
}
return ans
}

View File

@ -0,0 +1,50 @@
package leetcode
import (
"fmt"
"testing"
)
type question492 struct {
para492
ans492
}
// area 是参数
type para492 struct {
area int
}
// ans 是答案
type ans492 struct {
ans []int
}
func Test_Problem492(t *testing.T) {
qs := []question492{
{
para492{4},
ans492{[]int{2, 2}},
},
{
para492{37},
ans492{[]int{37, 1}},
},
{
para492{122122},
ans492{[]int{427, 286}},
},
}
fmt.Printf("------------------------Leetcode Problem 492------------------------\n")
for _, q := range qs {
_, p := q.ans492, q.para492
fmt.Printf("【input】:%v 【output】:%v\n", p, constructRectangle(p.area))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,72 @@
# [492. Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)
## 题目
A web developer needs to know how to design a web page's size.
So, given a specific rectangular web pages area, your job by now is to design a rectangular web page,
whose length L and width W satisfy the following requirements:
The area of the rectangular web page you designed must equal to the given target area.
The width W should not be larger than the length L, which means L >= W.
The difference between length L and width W should be as small as possible.
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
**Example 1:**
Input: area = 4
Output: [2,2]
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
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.
**Example 2:**
Input: area = 37
Output: [37,1]
**Example 3:**
Input: area = 122122
Output: [427,286]
**Constraints**
- 1 <= area <= 10000000
## 题目大意
作为一位 web 开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
1. 你设计的矩形页面必须等于给定的目标面积。
2. 宽度 W 不应大于长度 L换言之要求 L >= W 。
3. 长度 L 和宽度 W 之间的差距应当尽可能小。
你需要按顺序输出你设计的页面的长度 L 和宽度 W。
## 解题思路
- 令 W 等于根号 area
- 在 W 大于等于 1 的情况下,判断 area%W 是否等于 0,如果不相等 W 就减 1 继续循环,如果相等就返回 [area/W, W]
## 代码
```go
package leetcode
import "math"
func constructRectangle(area int) []int {
ans := make([]int, 2)
W := int(math.Sqrt(float64(area)))
for W >= 1 {
if area%W == 0 {
ans[0], ans[1] = area/W, W
break
}
W -= 1
}
return ans
}
``

View File

@ -87,5 +87,5 @@ func generateIncSubsets(nums []int, current int, c []int, res *[][]int) {
---------------------------------------------- ----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;"> <div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0485.Max-Consecutive-Ones/">⬅️上一页</a></p> <p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0485.Max-Consecutive-Ones/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0493.Reverse-Pairs/">下一页➡️</a></p> <p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0492.Construct-the-Rectangle/">下一页➡️</a></p>
</div> </div>

View File

@ -0,0 +1,79 @@
# [492. Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)
## 题目
A web developer needs to know how to design a web page's size.
So, given a specific rectangular web pages area, your job by now is to design a rectangular web page,
whose length L and width W satisfy the following requirements:
The area of the rectangular web page you designed must equal to the given target area.
The width W should not be larger than the length L, which means L >= W.
The difference between length L and width W should be as small as possible.
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
**Example 1:**
Input: area = 4
Output: [2,2]
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
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.
**Example 2:**
Input: area = 37
Output: [37,1]
**Example 3:**
Input: area = 122122
Output: [427,286]
**Constraints**
- 1 <= area <= 10000000
## 题目大意
作为一位 web 开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:
1. 你设计的矩形页面必须等于给定的目标面积。
2. 宽度 W 不应大于长度 L换言之要求 L >= W 。
3. 长度 L 和宽度 W 之间的差距应当尽可能小。
你需要按顺序输出你设计的页面的长度 L 和宽度 W。
## 解题思路
- 令 W 等于根号 area
- 在 W 大于等于 1 的情况下,判断 area%W 是否等于 0,如果不相等 W 就减 1 继续循环,如果相等就返回 [area/W, W]
## 代码
```go
package leetcode
import "math"
func constructRectangle(area int) []int {
ans := make([]int, 2)
W := int(math.Sqrt(float64(area)))
for W >= 1 {
if area%W == 0 {
ans[0], ans[1] = area/W, W
break
}
W -= 1
}
return ans
}
``
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0491.Increasing-Subsequences/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0493.Reverse-Pairs/">下一页➡️</a></p>
</div>

View File

@ -158,6 +158,6 @@ func reversePairs2(nums []int) int {
---------------------------------------------- ----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;"> <div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0491.Increasing-Subsequences/">⬅️上一页</a></p> <p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0492.Construct-the-Rectangle/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0494.Target-Sum/">下一页➡️</a></p> <p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0494.Target-Sum/">下一页➡️</a></p>
</div> </div>

View File

@ -62,6 +62,7 @@ weight: 12
|0477|Total Hamming Distance|[Go]({{< relref "/ChapterFour/0400~0499/0477.Total-Hamming-Distance.md" >}})|Medium||||51.5%| |0477|Total Hamming Distance|[Go]({{< relref "/ChapterFour/0400~0499/0477.Total-Hamming-Distance.md" >}})|Medium||||51.5%|
|0478|Generate Random Point in a Circle|[Go]({{< relref "/ChapterFour/0400~0499/0478.Generate-Random-Point-in-a-Circle.md" >}})|Medium||||39.1%| |0478|Generate Random Point in a Circle|[Go]({{< relref "/ChapterFour/0400~0499/0478.Generate-Random-Point-in-a-Circle.md" >}})|Medium||||39.1%|
|0483|Smallest Good Base|[Go]({{< relref "/ChapterFour/0400~0499/0483.Smallest-Good-Base.md" >}})|Hard||||37.3%| |0483|Smallest Good Base|[Go]({{< relref "/ChapterFour/0400~0499/0483.Smallest-Good-Base.md" >}})|Hard||||37.3%|
|0492|Construct the Rectangle|[Go]({{< relref "/ChapterFour/0400~0499/0492.Construct-the-Rectangle.md" >}})|Easy||||51.8%|
|0497|Random Point in Non-overlapping Rectangles|[Go]({{< relref "/ChapterFour/0400~0499/0497.Random-Point-in-Non-overlapping-Rectangles.md" >}})|Medium||||39.1%| |0497|Random Point in Non-overlapping Rectangles|[Go]({{< relref "/ChapterFour/0400~0499/0497.Random-Point-in-Non-overlapping-Rectangles.md" >}})|Medium||||39.1%|
|0507|Perfect Number|[Go]({{< relref "/ChapterFour/0500~0599/0507.Perfect-Number.md" >}})|Easy||||37.2%| |0507|Perfect Number|[Go]({{< relref "/ChapterFour/0500~0599/0507.Perfect-Number.md" >}})|Easy||||37.2%|
|0509|Fibonacci Number|[Go]({{< relref "/ChapterFour/0500~0599/0509.Fibonacci-Number.md" >}})|Easy||||67.8%| |0509|Fibonacci Number|[Go]({{< relref "/ChapterFour/0500~0599/0509.Fibonacci-Number.md" >}})|Easy||||67.8%|