From b3225a0f118ed470c8c65523a8e1127ad1e92888 Mon Sep 17 00:00:00 2001 From: halfrost Date: Sat, 6 Nov 2021 19:18:25 -0700 Subject: [PATCH] Add solution 492 --- README.md | 4 +- .../492.Construct the Rectangle.go | 16 ++++ .../492.Construct the Rectangle_test.go | 50 ++++++++++++ .../0492.Construct-the-Rectangle/README.md | 72 +++++++++++++++++ .../0400~0499/0491.Increasing-Subsequences.md | 2 +- .../0400~0499/0492.Construct-the-Rectangle.md | 79 +++++++++++++++++++ .../0400~0499/0493.Reverse-Pairs.md | 2 +- website/content/ChapterTwo/Math.md | 1 + 8 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle.go create mode 100644 leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle_test.go create mode 100644 leetcode/0492.Construct-the-Rectangle/README.md create mode 100644 website/content/ChapterFour/0400~0499/0492.Construct-the-Rectangle.md diff --git a/README.md b/README.md index 30d2058f..e15b34f6 100755 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ ## 二. 目录 -以下已经收录了 723 道题的题解,还有 11 道题在尝试优化到 beats 100% +以下已经收录了 724 道题的题解,还有 11 道题在尝试优化到 beats 100% | No. | Title | Solution | Acceptance | Difficulty | Frequency | |:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:| @@ -630,7 +630,7 @@ |0489|Robot Room Cleaner||74.6%|Hard|| |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|| -|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|| |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|| diff --git a/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle.go b/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle.go new file mode 100644 index 00000000..3314fbc1 --- /dev/null +++ b/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle.go @@ -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 +} diff --git a/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle_test.go b/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle_test.go new file mode 100644 index 00000000..b3ed9839 --- /dev/null +++ b/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle_test.go @@ -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") +} diff --git a/leetcode/0492.Construct-the-Rectangle/README.md b/leetcode/0492.Construct-the-Rectangle/README.md new file mode 100644 index 00000000..8024d6e1 --- /dev/null +++ b/leetcode/0492.Construct-the-Rectangle/README.md @@ -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 page’s 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 +} + +`` \ No newline at end of file diff --git a/website/content/ChapterFour/0400~0499/0491.Increasing-Subsequences.md b/website/content/ChapterFour/0400~0499/0491.Increasing-Subsequences.md index a7271f6a..5958187a 100755 --- a/website/content/ChapterFour/0400~0499/0491.Increasing-Subsequences.md +++ b/website/content/ChapterFour/0400~0499/0491.Increasing-Subsequences.md @@ -87,5 +87,5 @@ func generateIncSubsets(nums []int, current int, c []int, res *[][]int) { ----------------------------------------------

⬅️上一页

-

下一页➡️

+

下一页➡️

diff --git a/website/content/ChapterFour/0400~0499/0492.Construct-the-Rectangle.md b/website/content/ChapterFour/0400~0499/0492.Construct-the-Rectangle.md new file mode 100644 index 00000000..5e02ec17 --- /dev/null +++ b/website/content/ChapterFour/0400~0499/0492.Construct-the-Rectangle.md @@ -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 page’s 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 +} + +`` + + +---------------------------------------------- +
+

⬅️上一页

+

下一页➡️

+
diff --git a/website/content/ChapterFour/0400~0499/0493.Reverse-Pairs.md b/website/content/ChapterFour/0400~0499/0493.Reverse-Pairs.md index 14256f76..3fffb378 100755 --- a/website/content/ChapterFour/0400~0499/0493.Reverse-Pairs.md +++ b/website/content/ChapterFour/0400~0499/0493.Reverse-Pairs.md @@ -158,6 +158,6 @@ func reversePairs2(nums []int) int { ----------------------------------------------
-

⬅️上一页

+

⬅️上一页

下一页➡️

diff --git a/website/content/ChapterTwo/Math.md b/website/content/ChapterTwo/Math.md index b41f60a3..2ccff6ee 100644 --- a/website/content/ChapterTwo/Math.md +++ b/website/content/ChapterTwo/Math.md @@ -62,6 +62,7 @@ weight: 12 |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%| |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%| |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%|