Skip to content

Commit 096da77

Browse files
committed
Add solution 1437
1 parent a6ffd52 commit 096da77

31 files changed

+627
-341
lines changed

README.md

Lines changed: 261 additions & 261 deletions
Large diffs are not rendered by default.

leetcode/0220.Contains-Duplicate-III/220. Contains Duplicate III_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func Test_Problem220(t *testing.T) {
5252
para220{[]int{1, 5, 9, 1, 5, 9}, 2, 3},
5353
ans220{false},
5454
},
55+
56+
{
57+
para220{[]int{1, 2, 1, 1}, 1, 0},
58+
ans220{true},
59+
},
5560
}
5661

5762
fmt.Printf("------------------------Leetcode Problem 220------------------------\n")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode
2+
3+
func kLengthApart(nums []int, k int) bool {
4+
prevIndex := -1
5+
for i, num := range nums {
6+
if num == 1 {
7+
if prevIndex != -1 && i-prevIndex-1 < k {
8+
return false
9+
}
10+
prevIndex = i
11+
}
12+
}
13+
return true
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1437 struct {
9+
para1437
10+
ans1437
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1437 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1437 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem1437(t *testing.T) {
27+
28+
qs := []question1437{
29+
30+
{
31+
para1437{[]int{1, 0, 0, 0, 1, 0, 0, 1}, 2},
32+
ans1437{true},
33+
},
34+
35+
{
36+
para1437{[]int{1, 0, 0, 1, 0, 1}, 2},
37+
ans1437{false},
38+
},
39+
40+
{
41+
para1437{[]int{1, 1, 1, 1, 1}, 0},
42+
ans1437{true},
43+
},
44+
45+
{
46+
para1437{[]int{0, 1, 0, 1}, 1},
47+
ans1437{true},
48+
},
49+
}
50+
51+
fmt.Printf("------------------------Leetcode Problem 1437------------------------\n")
52+
53+
for _, q := range qs {
54+
_, p := q.ans1437, q.para1437
55+
fmt.Printf("【input】:%v ", p)
56+
fmt.Printf("【output】:%v \n", kLengthApart(p.nums, p.k))
57+
}
58+
fmt.Printf("\n\n\n")
59+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [1437. Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)
2+
3+
4+
## 题目
5+
6+
Given an array `nums` of 0s and 1s and an integer `k`, return `True` if all 1's are at least `k` places away from each other, otherwise return `False`.
7+
8+
**Example 1:**
9+
10+
![https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png](https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png)
11+
12+
```
13+
Input: nums = [1,0,0,0,1,0,0,1], k = 2
14+
Output: true
15+
Explanation: Each of the 1s are at least 2 places away from each other.
16+
```
17+
18+
**Example 2:**
19+
20+
![https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png](https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png)
21+
22+
```
23+
Input: nums = [1,0,0,1,0,1], k = 2
24+
Output: false
25+
Explanation: The second 1 and third 1 are only one apart from each other.
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: nums = [1,1,1,1,1], k = 0
32+
Output: true
33+
```
34+
35+
**Example 4:**
36+
37+
```
38+
Input: nums = [0,1,0,1], k = 1
39+
Output: true
40+
```
41+
42+
**Constraints:**
43+
44+
- `1 <= nums.length <= 10^5`
45+
- `0 <= k <= nums.length`
46+
- `nums[i]` is `0` or `1`
47+
48+
## 题目大意
49+
50+
给你一个由若干 0 和 1 组成的数组 nums 以及整数 k。如果所有 1 都至少相隔 k 个元素,则返回 True ;否则,返回 False 。
51+
52+
## 解题思路
53+
54+
- 简单题。扫描一遍数组,遇到 1 的时候比较前一个 1 的下标索引,如果相隔小于 k 则返回 false。如果大于等于 k 就更新下标索引,继续循环。循环结束输出 true 即可。
55+
56+
## 代码
57+
58+
```go
59+
package leetcode
60+
61+
func kLengthApart(nums []int, k int) bool {
62+
prevIndex := -1
63+
for i, num := range nums {
64+
if num == 1 {
65+
if prevIndex != -1 && i-prevIndex-1 < k {
66+
return false
67+
}
68+
prevIndex = i
69+
}
70+
}
71+
return true
72+
}
73+
```

website/content/ChapterFour/1389.Create-Target-Array-in-the-Given-Order.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ func createTargetArray(nums []int, index []int) []int {
9393
----------------------------------------------
9494
<div style="display: flex;justify-content: space-between;align-items: center;">
9595
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1385.Find-the-Distance-Value-Between-Two-Arrays/">⬅️上一页</a></p>
96-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/">下一页➡️</a></p>
96+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/">下一页➡️</a></p>
9797
</div>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [1437. Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)
2+
3+
4+
## 题目
5+
6+
Given an array `nums` of 0s and 1s and an integer `k`, return `True` if all 1's are at least `k` places away from each other, otherwise return `False`.
7+
8+
**Example 1:**
9+
10+
![https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png](https://assets.leetcode.com/uploads/2020/04/15/sample_1_1791.png)
11+
12+
```
13+
Input: nums = [1,0,0,0,1,0,0,1], k = 2
14+
Output: true
15+
Explanation: Each of the 1s are at least 2 places away from each other.
16+
```
17+
18+
**Example 2:**
19+
20+
![https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png](https://assets.leetcode.com/uploads/2020/04/15/sample_2_1791.png)
21+
22+
```
23+
Input: nums = [1,0,0,1,0,1], k = 2
24+
Output: false
25+
Explanation: The second 1 and third 1 are only one apart from each other.
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: nums = [1,1,1,1,1], k = 0
32+
Output: true
33+
```
34+
35+
**Example 4:**
36+
37+
```
38+
Input: nums = [0,1,0,1], k = 1
39+
Output: true
40+
```
41+
42+
**Constraints:**
43+
44+
- `1 <= nums.length <= 10^5`
45+
- `0 <= k <= nums.length`
46+
- `nums[i]` is `0` or `1`
47+
48+
## 题目大意
49+
50+
给你一个由若干 0 和 1 组成的数组 nums 以及整数 k。如果所有 1 都至少相隔 k 个元素,则返回 True ;否则,返回 False 。
51+
52+
## 解题思路
53+
54+
- 简单题。扫描一遍数组,遇到 1 的时候比较前一个 1 的下标索引,如果相隔小于 k 则返回 false。如果大于等于 k 就更新下标索引,继续循环。循环结束输出 true 即可。
55+
56+
## 代码
57+
58+
```go
59+
package leetcode
60+
61+
func kLengthApart(nums []int, k int) bool {
62+
prevIndex := -1
63+
for i, num := range nums {
64+
if num == 1 {
65+
if prevIndex != -1 && i-prevIndex-1 < k {
66+
return false
67+
}
68+
prevIndex = i
69+
}
70+
}
71+
return true
72+
}
73+
```
74+
75+
76+
----------------------------------------------
77+
<div style="display: flex;justify-content: space-between;align-items: center;">
78+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1389.Create-Target-Array-in-the-Given-Order/">⬅️上一页</a></p>
79+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/">下一页➡️</a></p>
80+
</div>

website/content/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ func isPrefixOfWord(sentence string, searchWord string) int {
100100

101101
----------------------------------------------
102102
<div style="display: flex;justify-content: space-between;align-items: center;">
103-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1389.Create-Target-Array-in-the-Given-Order/">⬅️上一页</a></p>
104-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1464.Maximum-Product-of-Two-Elements-in-an-Array/">下一页➡️</a></p>
103+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/">⬅️上一页</a></p>
104+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1463.Cherry-Pickup-II/">下一页➡️</a></p>
105105
</div>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [1463. Cherry Pickup II](https://leetcode.com/problems/cherry-pickup-ii/)
2+
Given a rows x cols matrix grid representing a field of cherries. Each cell in grid represents the number of cherries that you can collect.
3+
4+
You have two robots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid.
5+
6+
Return the maximum number of cherries collection using both robots by following the rules below:
7+
8+
From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1).
9+
When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).
10+
When both robots stay on the same cell, only one of them takes the cherries.
11+
Both robots cannot move outside of the grid at any moment.
12+
Both robots should reach the bottom row in the grid.
13+
14+
## Example 1:
15+
```
16+
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
17+
Output: 24
18+
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
19+
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
20+
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
21+
Total of cherries: 12 + 12 = 24.
22+
```
23+
24+
## Example 2:
25+
```
26+
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
27+
Output: 28
28+
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
29+
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
30+
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
31+
Total of cherries: 17 + 11 = 28.
32+
```
33+
34+
## Example 3:
35+
```
36+
Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]]
37+
Output: 22
38+
```
39+
40+
## Example 4:
41+
```
42+
Input: grid = [[1,1],[1,1]]
43+
Output: 4
44+
```
45+
46+
47+
----------------------------------------------
48+
<div style="display: flex;justify-content: space-between;align-items: center;">
49+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/">⬅️上一页</a></p>
50+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1464.Maximum-Product-of-Two-Elements-in-an-Array/">下一页➡️</a></p>
51+
</div>

website/content/ChapterFour/1464.Maximum-Product-of-Two-Elements-in-an-Array.md

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

6969
----------------------------------------------
7070
<div style="display: flex;justify-content: space-between;align-items: center;">
71-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/">⬅️上一页</a></p>
71+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1463.Cherry-Pickup-II/">⬅️上一页</a></p>
7272
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1470.Shuffle-the-Array/">下一页➡️</a></p>
7373
</div>

0 commit comments

Comments
 (0)