Skip to content

Commit 8ded561

Browse files
committed
Add solution 0667
1 parent a8f9bfb commit 8ded561

26 files changed

+641
-445
lines changed

README.md

+314-314
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func constructArray(n int, k int) []int {
4+
res := []int{}
5+
for i := 0; i < n-k-1; i++ {
6+
res = append(res, i+1)
7+
}
8+
for i := n - k; i < n-k+(k+1)/2; i++ {
9+
res = append(res, i)
10+
res = append(res, 2*n-k-i)
11+
}
12+
if k%2 == 0 {
13+
res = append(res, n-k+(k+1)/2)
14+
}
15+
return res
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question667 struct {
9+
para667
10+
ans667
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para667 struct {
16+
n int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans667 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem667(t *testing.T) {
27+
28+
qs := []question667{
29+
30+
{
31+
para667{3, 1},
32+
ans667{[]int{1, 2, 3}},
33+
},
34+
35+
{
36+
para667{3, 2},
37+
ans667{[]int{1, 3, 2}},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 667------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans667, q.para667
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, constructArray(p.n, p.k))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [667. Beautiful Arrangement II](https://leetcode.com/problems/beautiful-arrangement-ii/)
2+
3+
4+
## 题目
5+
6+
Given two integers `n` and `k`, you need to construct a list which contains `n` different positive integers ranging from `1` to `n` and obeys the following requirement:Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly `k` distinct integers.
7+
8+
If there are multiple answers, print any of them.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: n = 3, k = 1
14+
Output: [1, 2, 3]
15+
Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: n = 3, k = 2
22+
Output: [1, 3, 2]
23+
Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.
24+
```
25+
26+
**Note:**
27+
28+
1. The `n` and `k` are in the range 1 <= k < n <= 10^4.
29+
30+
## 题目大意
31+
32+
给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件:
33+
34+
- 如果这个数组是 [a1, a2, a3, ... , an] ,那么数组 [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] 中应该有且仅有 k 个不同整数;.
35+
- 如果存在多种答案,你只需实现并返回其中任意一种.
36+
37+
## 解题思路
38+
39+
- 先考虑 `k` 最大值的情况。如果把末尾的较大值依次插入到前面的较小值中,形成 `[1,n,2,n-1,3,n-2,……]`,这样排列 `k` 能取到最大值 `n-1``k` 最小值的情况是 `[1,2,3,4,……,n]``k` 取到的最小值是 1。那么 `k``[1,n-1]` 之间取值,该怎么排列呢?先顺序排列 `[1,2,3,4,……,n-k-1]`,这里有 `n-k-1` 个数,可以形成唯一一种差值。剩下 `k+1` 个数,形成 `k-1` 种差值。
40+
- 这又回到了 `k` 最大值的取法了。`k` 取最大值的情况是 `n` 个数,形成 `n-1` 个不同种的差值。现在 `k+1` 个数,需要形成 `k` 种不同的差值。两者是同一个问题。那么剩下 `k` 个数的排列方法是 `[n-k,n-k+1,…,n]`,这里有 `k` 个数,注意代码实现时,注意 `k` 的奇偶性,如果 `k` 是奇数,“对半穿插”以后,正好匹配完,如果 `k` 是偶数,对半处的数 `n-k+(k+1)/2`,最后还需要单独加入到排列中。
41+
- 可能有读者会问了,前面生成了 1 种差值,后面这部分又生产了 `k` 种差值,加起来不是 `k + 1` 种差值了么?这种理解是错误的。后面这段最后 2 个数字是 `n-k+(k+1)/2-1``n-k+(k+1)/2`,它们两者的差值是 1,和第一段构造的排列差值是相同的,都是 1。所以第一段构造了 1 种差值,第二段虽然构造了 `k` 种,但是需要去掉两段重复的差值 1,所以最终差值种类还是 `1 + k - 1 = k` 种。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
func constructArray(n int, k int) []int {
49+
res := []int{}
50+
for i := 0; i < n-k-1; i++ {
51+
res = append(res, i+1)
52+
}
53+
for i := n - k; i < n-k+(k+1)/2; i++ {
54+
res = append(res, i)
55+
res = append(res, 2*n-k-i)
56+
}
57+
if k%2 == 0 {
58+
res = append(res, n-k+(k+1)/2)
59+
}
60+
return res
61+
}
62+
```

website/content/ChapterFour/0600~0699/0665.Non-decreasing-Array.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ func checkPossibility(nums []int) bool {
6262
----------------------------------------------
6363
<div style="display: flex;justify-content: space-between;align-items: center;">
6464
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0662.Maximum-Width-of-Binary-Tree/">⬅️上一页</a></p>
65-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0668.Kth-Smallest-Number-in-Multiplication-Table/">下一页➡️</a></p>
65+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0667.Beautiful-Arrangement-II/">下一页➡️</a></p>
6666
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [667. Beautiful Arrangement II](https://leetcode.com/problems/beautiful-arrangement-ii/)
2+
3+
4+
## 题目
5+
6+
Given two integers `n` and `k`, you need to construct a list which contains `n` different positive integers ranging from `1` to `n` and obeys the following requirement:Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly `k` distinct integers.
7+
8+
If there are multiple answers, print any of them.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: n = 3, k = 1
14+
Output: [1, 2, 3]
15+
Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: n = 3, k = 2
22+
Output: [1, 3, 2]
23+
Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.
24+
```
25+
26+
**Note:**
27+
28+
1. The `n` and `k` are in the range 1 <= k < n <= 10^4.
29+
30+
## 题目大意
31+
32+
给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件:
33+
34+
- 如果这个数组是 [a1, a2, a3, ... , an] ,那么数组 [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] 中应该有且仅有 k 个不同整数;.
35+
- 如果存在多种答案,你只需实现并返回其中任意一种.
36+
37+
## 解题思路
38+
39+
- 先考虑 `k` 最大值的情况。如果把末尾的较大值依次插入到前面的较小值中,形成 `[1,n,2,n-1,3,n-2,……]`,这样排列 `k` 能取到最大值 `n-1``k` 最小值的情况是 `[1,2,3,4,……,n]``k` 取到的最小值是 1。那么 `k``[1,n-1]` 之间取值,该怎么排列呢?先顺序排列 `[1,2,3,4,……,n-k-1]`,这里有 `n-k-1` 个数,可以形成唯一一种差值。剩下 `k+1` 个数,形成 `k-1` 种差值。
40+
- 这又回到了 `k` 最大值的取法了。`k` 取最大值的情况是 `n` 个数,形成 `n-1` 个不同种的差值。现在 `k+1` 个数,需要形成 `k` 种不同的差值。两者是同一个问题。那么剩下 `k` 个数的排列方法是 `[n-k,n-k+1,…,n]`,这里有 `k` 个数,注意代码实现时,注意 `k` 的奇偶性,如果 `k` 是奇数,“对半穿插”以后,正好匹配完,如果 `k` 是偶数,对半处的数 `n-k+(k+1)/2`,最后还需要单独加入到排列中。
41+
- 可能有读者会问了,前面生成了 1 种差值,后面这部分又生产了 `k` 种差值,加起来不是 `k + 1` 种差值了么?这种理解是错误的。后面这段最后 2 个数字是 `n-k+(k+1)/2-1``n-k+(k+1)/2`,它们两者的差值是 1,和第一段构造的排列差值是相同的,都是 1。所以第一段构造了 1 种差值,第二段虽然构造了 `k` 种,但是需要去掉两段重复的差值 1,所以最终差值种类还是 `1 + k - 1 = k` 种。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
func constructArray(n int, k int) []int {
49+
res := []int{}
50+
for i := 0; i < n-k-1; i++ {
51+
res = append(res, i+1)
52+
}
53+
for i := n - k; i < n-k+(k+1)/2; i++ {
54+
res = append(res, i)
55+
res = append(res, 2*n-k-i)
56+
}
57+
if k%2 == 0 {
58+
res = append(res, n-k+(k+1)/2)
59+
}
60+
return res
61+
}
62+
```
63+
64+
65+
----------------------------------------------
66+
<div style="display: flex;justify-content: space-between;align-items: center;">
67+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0665.Non-decreasing-Array/">⬅️上一页</a></p>
68+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0668.Kth-Smallest-Number-in-Multiplication-Table/">下一页➡️</a></p>
69+
</div>

website/content/ChapterFour/0600~0699/0668.Kth-Smallest-Number-in-Multiplication-Table.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ func counterKthNum(m, n, mid int) int {
8686

8787
----------------------------------------------
8888
<div style="display: flex;justify-content: space-between;align-items: center;">
89-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0665.Non-decreasing-Array/">⬅️上一页</a></p>
89+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0667.Beautiful-Arrangement-II/">⬅️上一页</a></p>
9090
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0669.Trim-a-Binary-Search-Tree/">下一页➡️</a></p>
9191
</div>

0 commit comments

Comments
 (0)