Skip to content

Commit bbb448d

Browse files
committed
Add solution 1640
1 parent 88a0452 commit bbb448d

File tree

5 files changed

+280
-0
lines changed

5 files changed

+280
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
func canFormArray(arr []int, pieces [][]int) bool {
4+
arrMap := map[int]int{}
5+
for i, v := range arr {
6+
arrMap[v] = i
7+
}
8+
for i := 0; i < len(pieces); i++ {
9+
order := -1
10+
for j := 0; j < len(pieces[i]); j++ {
11+
if _, ok := arrMap[pieces[i][j]]; !ok {
12+
return false
13+
}
14+
if order == -1 {
15+
order = arrMap[pieces[i][j]]
16+
} else {
17+
if arrMap[pieces[i][j]] == order+1 {
18+
order = arrMap[pieces[i][j]]
19+
} else {
20+
return false
21+
}
22+
}
23+
}
24+
}
25+
return true
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1640 struct {
9+
para1640
10+
ans1640
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1640 struct {
16+
arr []int
17+
pieces [][]int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1640 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem1640(t *testing.T) {
27+
28+
qs := []question1640{
29+
30+
{
31+
para1640{[]int{85}, [][]int{{85}}},
32+
ans1640{true},
33+
},
34+
35+
{
36+
para1640{[]int{15, 88}, [][]int{{88}, {15}}},
37+
ans1640{true},
38+
},
39+
40+
{
41+
para1640{[]int{49, 18, 16}, [][]int{{16, 18, 49}}},
42+
ans1640{false},
43+
},
44+
45+
{
46+
para1640{[]int{91, 4, 64, 78}, [][]int{{78}, {4, 64}, {91}}},
47+
ans1640{true},
48+
},
49+
50+
{
51+
para1640{[]int{1, 3, 5, 7}, [][]int{{2, 4, 6, 8}}},
52+
ans1640{false},
53+
},
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 1640------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans1640, q.para1640
60+
fmt.Printf("【input】:%v 【output】:%v \n", p, canFormArray(p.arr, p.pieces))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [1640. Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)
2+
3+
4+
## 题目
5+
6+
You are given an array of **distinct** integers `arr` and an array of integer arrays `pieces`, where the integers in `pieces` are **distinct**. Your goal is to form `arr` by concatenating the arrays in `pieces` **in any order**. However, you are **not** allowed to reorder the integers in each array `pieces[i]`.
7+
8+
Return `true` *if it is possible to form the array* `arr` *from* `pieces`. Otherwise, return `false`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: arr = [85], pieces = [[85]]
14+
Output: true
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: arr = [15,88], pieces = [[88],[15]]
21+
Output: true
22+
Explanation: Concatenate [15] then [88]
23+
```
24+
25+
**Example 3:**
26+
27+
```
28+
Input: arr = [49,18,16], pieces = [[16,18,49]]
29+
Output: false
30+
Explanation: Even though the numbers match, we cannot reorder pieces[0].
31+
```
32+
33+
**Example 4:**
34+
35+
```
36+
Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
37+
Output: true
38+
Explanation: Concatenate [91] then [4,64] then [78]
39+
```
40+
41+
**Example 5:**
42+
43+
```
44+
Input: arr = [1,3,5,7], pieces = [[2,4,6,8]]
45+
Output: false
46+
47+
```
48+
49+
**Constraints:**
50+
51+
- `1 <= pieces.length <= arr.length <= 100`
52+
- `sum(pieces[i].length) == arr.length`
53+
- `1 <= pieces[i].length <= arr.length`
54+
- `1 <= arr[i], pieces[i][j] <= 100`
55+
- The integers in `arr` are **distinct**.
56+
- The integers in `pieces` are **distinct** (i.e., If we flatten pieces in a 1D array, all the integers in this array are distinct).
57+
58+
## 题目大意
59+
60+
给你一个整数数组 arr ,数组中的每个整数 互不相同 。另有一个由整数数组构成的数组 pieces,其中的整数也 互不相同 。请你以 任意顺序 连接 pieces 中的数组以形成 arr 。但是,不允许 对每个数组 pieces[i] 中的整数重新排序。如果可以连接 pieces 中的数组形成 arr ,返回 true ;否则,返回 false 。
61+
62+
## 解题思路
63+
64+
- 简单题。题目保证了 arr 中的元素唯一,所以可以用 map 把每个元素的 index 存起来,方便查找。遍历 pieces 数组,在每个一维数组中判断元素顺序是否和原 arr 元素相对顺序一致。这个时候就用 map 查找,如果顺序是一一相连的,那么就是正确的。有一个顺序不是一一相连,或者出现了 arr 不存在的元素,都返回 false。
65+
66+
## 代码
67+
68+
```go
69+
package leetcode
70+
71+
func canFormArray(arr []int, pieces [][]int) bool {
72+
arrMap := map[int]int{}
73+
for i, v := range arr {
74+
arrMap[v] = i
75+
}
76+
for i := 0; i < len(pieces); i++ {
77+
order := -1
78+
for j := 0; j < len(pieces[i]); j++ {
79+
if _, ok := arrMap[pieces[i][j]]; !ok {
80+
return false
81+
}
82+
if order == -1 {
83+
order = arrMap[pieces[i][j]]
84+
} else {
85+
if arrMap[pieces[i][j]] == order+1 {
86+
order = arrMap[pieces[i][j]]
87+
} else {
88+
return false
89+
}
90+
}
91+
}
92+
}
93+
return true
94+
}
95+
```
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [1640. Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)
2+
3+
4+
## 题目
5+
6+
You are given an array of **distinct** integers `arr` and an array of integer arrays `pieces`, where the integers in `pieces` are **distinct**. Your goal is to form `arr` by concatenating the arrays in `pieces` **in any order**. However, you are **not** allowed to reorder the integers in each array `pieces[i]`.
7+
8+
Return `true` *if it is possible to form the array* `arr` *from* `pieces`. Otherwise, return `false`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: arr = [85], pieces = [[85]]
14+
Output: true
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: arr = [15,88], pieces = [[88],[15]]
21+
Output: true
22+
Explanation: Concatenate [15] then [88]
23+
```
24+
25+
**Example 3:**
26+
27+
```
28+
Input: arr = [49,18,16], pieces = [[16,18,49]]
29+
Output: false
30+
Explanation: Even though the numbers match, we cannot reorder pieces[0].
31+
```
32+
33+
**Example 4:**
34+
35+
```
36+
Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
37+
Output: true
38+
Explanation: Concatenate [91] then [4,64] then [78]
39+
```
40+
41+
**Example 5:**
42+
43+
```
44+
Input: arr = [1,3,5,7], pieces = [[2,4,6,8]]
45+
Output: false
46+
47+
```
48+
49+
**Constraints:**
50+
51+
- `1 <= pieces.length <= arr.length <= 100`
52+
- `sum(pieces[i].length) == arr.length`
53+
- `1 <= pieces[i].length <= arr.length`
54+
- `1 <= arr[i], pieces[i][j] <= 100`
55+
- The integers in `arr` are **distinct**.
56+
- The integers in `pieces` are **distinct** (i.e., If we flatten pieces in a 1D array, all the integers in this array are distinct).
57+
58+
## 题目大意
59+
60+
给你一个整数数组 arr ,数组中的每个整数 互不相同 。另有一个由整数数组构成的数组 pieces,其中的整数也 互不相同 。请你以 任意顺序 连接 pieces 中的数组以形成 arr 。但是,不允许 对每个数组 pieces[i] 中的整数重新排序。如果可以连接 pieces 中的数组形成 arr ,返回 true ;否则,返回 false 。
61+
62+
## 解题思路
63+
64+
- 简单题。题目保证了 arr 中的元素唯一,所以可以用 map 把每个元素的 index 存起来,方便查找。遍历 pieces 数组,在每个一维数组中判断元素顺序是否和原 arr 元素相对顺序一致。这个时候就用 map 查找,如果顺序是一一相连的,那么就是正确的。有一个顺序不是一一相连,或者出现了 arr 不存在的元素,都返回 false。
65+
66+
## 代码
67+
68+
```go
69+
package leetcode
70+
71+
func canFormArray(arr []int, pieces [][]int) bool {
72+
arrMap := map[int]int{}
73+
for i, v := range arr {
74+
arrMap[v] = i
75+
}
76+
for i := 0; i < len(pieces); i++ {
77+
order := -1
78+
for j := 0; j < len(pieces[i]); j++ {
79+
if _, ok := arrMap[pieces[i][j]]; !ok {
80+
return false
81+
}
82+
if order == -1 {
83+
order = arrMap[pieces[i][j]]
84+
} else {
85+
if arrMap[pieces[i][j]] == order+1 {
86+
order = arrMap[pieces[i][j]]
87+
} else {
88+
return false
89+
}
90+
}
91+
}
92+
}
93+
return true
94+
}
95+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ headless: true
551551
- [1480.Running-Sum-of-1d-Array]({{< relref "/ChapterFour/1480.Running-Sum-of-1d-Array.md" >}})
552552
- [1512.Number-of-Good-Pairs]({{< relref "/ChapterFour/1512.Number-of-Good-Pairs.md" >}})
553553
- [1573.Number-of-Ways-to-Split-a-String]({{< relref "/ChapterFour/1573.Number-of-Ways-to-Split-a-String.md" >}})
554+
- [1640.Check-Array-Formation-Through-Concatenation]({{< relref "/ChapterFour/1640.Check-Array-Formation-Through-Concatenation.md" >}})
554555
- [1646.Get-Maximum-in-Generated-Array]({{< relref "/ChapterFour/1646.Get-Maximum-in-Generated-Array.md" >}})
555556
- [1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique]({{< relref "/ChapterFour/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique.md" >}})
556557
- [1648.Sell-Diminishing-Valued-Colored-Balls]({{< relref "/ChapterFour/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})

0 commit comments

Comments
 (0)