From 88a0452f2622ca23fec627ad48db5a433eec8395 Mon Sep 17 00:00:00 2001 From: YDZ Date: Fri, 1 Jan 2021 21:21:21 +0800 Subject: [PATCH] Add solution 605 --- .../605. Can Place Flowers.go | 18 +++++ .../605. Can Place Flowers_test.go | 68 +++++++++++++++++++ leetcode/0605.Can-Place-Flowers/README.md | 60 ++++++++++++++++ leetcode/0910.Smallest-Range-II/README.md | 6 +- .../ChapterFour/0605.Can-Place-Flowers.md | 60 ++++++++++++++++ website/content/menu/index.md | 1 + 6 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 leetcode/0605.Can-Place-Flowers/605. Can Place Flowers.go create mode 100644 leetcode/0605.Can-Place-Flowers/605. Can Place Flowers_test.go create mode 100644 leetcode/0605.Can-Place-Flowers/README.md create mode 100644 website/content/ChapterFour/0605.Can-Place-Flowers.md diff --git a/leetcode/0605.Can-Place-Flowers/605. Can Place Flowers.go b/leetcode/0605.Can-Place-Flowers/605. Can Place Flowers.go new file mode 100644 index 00000000..0b598096 --- /dev/null +++ b/leetcode/0605.Can-Place-Flowers/605. Can Place Flowers.go @@ -0,0 +1,18 @@ +package leetcode + +func canPlaceFlowers(flowerbed []int, n int) bool { + lenth := len(flowerbed) + for i := 0; i < lenth && n > 0; i += 2 { + if flowerbed[i] == 0 { + if i+1 == lenth || flowerbed[i+1] == 0 { + n-- + } else { + i++ + } + } + } + if n == 0 { + return true + } + return false +} diff --git a/leetcode/0605.Can-Place-Flowers/605. Can Place Flowers_test.go b/leetcode/0605.Can-Place-Flowers/605. Can Place Flowers_test.go new file mode 100644 index 00000000..0b7ce7f2 --- /dev/null +++ b/leetcode/0605.Can-Place-Flowers/605. Can Place Flowers_test.go @@ -0,0 +1,68 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question605 struct { + para605 + ans605 +} + +// para 是参数 +// one 代表第一个参数 +type para605 struct { + flowerbed []int + n int +} + +// ans 是答案 +// one 代表第一个答案 +type ans605 struct { + one bool +} + +func Test_Problem605(t *testing.T) { + + qs := []question605{ + + { + para605{[]int{1, 0, 0, 0, 1}, 1}, + ans605{true}, + }, + + { + para605{[]int{1, 0, 0, 0, 1}, 2}, + ans605{false}, + }, + + { + para605{[]int{1, 0, 0, 0, 0, 1}, 2}, + ans605{false}, + }, + + { + para605{[]int{0, 0, 1, 0}, 1}, + ans605{true}, + }, + + { + para605{[]int{0, 0, 1, 0, 0}, 1}, + ans605{true}, + }, + + { + para605{[]int{1, 0, 0, 1, 0}, 2}, + ans605{false}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 605------------------------\n") + + for _, q := range qs { + _, p := q.ans605, q.para605 + fmt.Printf("【input】:%v 【output】:%v\n", p, canPlaceFlowers(p.flowerbed, p.n)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/0605.Can-Place-Flowers/README.md b/leetcode/0605.Can-Place-Flowers/README.md new file mode 100644 index 00000000..746817ad --- /dev/null +++ b/leetcode/0605.Can-Place-Flowers/README.md @@ -0,0 +1,60 @@ +# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) + +## 题目 + +You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in **adjacent** plots. + +Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return *if* `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule. + +**Example 1:** + +``` +Input: flowerbed = [1,0,0,0,1], n = 1 +Output: true +``` + +**Example 2:** + +``` +Input: flowerbed = [1,0,0,0,1], n = 2 +Output: false +``` + +**Constraints:** + +- `1 <= flowerbed.length <= 2 * 104` +- `flowerbed[i]` is `0` or `1`. +- There are no two adjacent flowers in `flowerbed`. +- `0 <= n <= flowerbed.length` + +## 题目大意 + +假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。 + +## 解题思路 + +- 这一题最容易想到的解法是步长为 2 遍历数组,依次计数 0 的个数。有 2 种特殊情况需要单独判断,第一种情况是首尾连续多个 0,例如,00001 和 10000,第二种情况是 2 个 1 中间存在的 0 不足以种花,例如,1001 和 100001,1001 不能种任何花,100001 只能种一种花。单独判断出这 2 种情况,这一题就可以 AC 了。 +- 换个思路,找到可以种花的基本单元是 00,那么上面那 2 种特殊情况都可以统一成一种情况。判断是否当前存在 00 的组合,如果存在 00 的组合,都可以种花。末尾的情况需要单独判断,如果末尾为 0,也可以种花。这个时候不需要再找 00 组合,因为会越界。代码实现如下,思路很简洁明了。 + +## 代码 + +```go +package leetcode + +func canPlaceFlowers(flowerbed []int, n int) bool { + lenth := len(flowerbed) + for i := 0; i < lenth && n > 0; i += 2 { + if flowerbed[i] == 0 { + if i+1 == lenth || flowerbed[i+1] == 0 { + n-- + } else { + i++ + } + } + } + if n == 0 { + return true + } + return false +} +``` \ No newline at end of file diff --git a/leetcode/0910.Smallest-Range-II/README.md b/leetcode/0910.Smallest-Range-II/README.md index 5aea9e54..e8a3d8ff 100644 --- a/leetcode/0910.Smallest-Range-II/README.md +++ b/leetcode/0910.Smallest-Range-II/README.md @@ -10,7 +10,7 @@ Return the smallest possible difference between the maximum value of `B` and t **Example 1:** -``` +```c Input: A = [1], K = 0 Output: 0 Explanation: B = [1] @@ -18,7 +18,7 @@ Explanation: B = [1] **Example 2:** -``` +```c Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] @@ -26,7 +26,7 @@ Explanation: B = [2,8] **Example 3:** -``` +```c Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3] diff --git a/website/content/ChapterFour/0605.Can-Place-Flowers.md b/website/content/ChapterFour/0605.Can-Place-Flowers.md new file mode 100644 index 00000000..746817ad --- /dev/null +++ b/website/content/ChapterFour/0605.Can-Place-Flowers.md @@ -0,0 +1,60 @@ +# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) + +## 题目 + +You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in **adjacent** plots. + +Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return *if* `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule. + +**Example 1:** + +``` +Input: flowerbed = [1,0,0,0,1], n = 1 +Output: true +``` + +**Example 2:** + +``` +Input: flowerbed = [1,0,0,0,1], n = 2 +Output: false +``` + +**Constraints:** + +- `1 <= flowerbed.length <= 2 * 104` +- `flowerbed[i]` is `0` or `1`. +- There are no two adjacent flowers in `flowerbed`. +- `0 <= n <= flowerbed.length` + +## 题目大意 + +假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。 + +## 解题思路 + +- 这一题最容易想到的解法是步长为 2 遍历数组,依次计数 0 的个数。有 2 种特殊情况需要单独判断,第一种情况是首尾连续多个 0,例如,00001 和 10000,第二种情况是 2 个 1 中间存在的 0 不足以种花,例如,1001 和 100001,1001 不能种任何花,100001 只能种一种花。单独判断出这 2 种情况,这一题就可以 AC 了。 +- 换个思路,找到可以种花的基本单元是 00,那么上面那 2 种特殊情况都可以统一成一种情况。判断是否当前存在 00 的组合,如果存在 00 的组合,都可以种花。末尾的情况需要单独判断,如果末尾为 0,也可以种花。这个时候不需要再找 00 组合,因为会越界。代码实现如下,思路很简洁明了。 + +## 代码 + +```go +package leetcode + +func canPlaceFlowers(flowerbed []int, n int) bool { + lenth := len(flowerbed) + for i := 0; i < lenth && n > 0; i += 2 { + if flowerbed[i] == 0 { + if i+1 == lenth || flowerbed[i+1] == 0 { + n-- + } else { + i++ + } + } + } + if n == 0 { + return true + } + return false +} +``` \ No newline at end of file diff --git a/website/content/menu/index.md b/website/content/menu/index.md index 307775be..71a9c59c 100644 --- a/website/content/menu/index.md +++ b/website/content/menu/index.md @@ -322,6 +322,7 @@ headless: true - [0594.Longest-Harmonious-Subsequence]({{< relref "/ChapterFour/0594.Longest-Harmonious-Subsequence.md" >}}) - [0598.Range-Addition-II]({{< relref "/ChapterFour/0598.Range-Addition-II.md" >}}) - [0599.Minimum-Index-Sum-of-Two-Lists]({{< relref "/ChapterFour/0599.Minimum-Index-Sum-of-Two-Lists.md" >}}) + - [0605.Can-Place-Flowers]({{< relref "/ChapterFour/0605.Can-Place-Flowers.md" >}}) - [0628.Maximum-Product-of-Three-Numbers]({{< relref "/ChapterFour/0628.Maximum-Product-of-Three-Numbers.md" >}}) - [0632.Smallest-Range-Covering-Elements-from-K-Lists]({{< relref "/ChapterFour/0632.Smallest-Range-Covering-Elements-from-K-Lists.md" >}}) - [0633.Sum-of-Square-Numbers]({{< relref "/ChapterFour/0633.Sum-of-Square-Numbers.md" >}})