Skip to content

Commit 53e7629

Browse files
committed
+ problem 1764
1 parent 621e81e commit 53e7629

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 1764. Form Array by Concatenating Subarrays of Another Array
2+
You are given a 2D integer array `groups` of length `n`. You are also given an integer array `nums`.
3+
4+
You are asked if you can choose `n` **disjoint** subarrays from the array `nums` such that the <code>i<sup>th</sup></code> subarray is equal to `groups[i]` (**0-indexed**), and if `i > 0`, the <code>(i-1)<sup>th</sup></code> subarray appears **before** the <code>i<sup>th</sup></code> subarray in `nums` (i.e. the subarrays must be in the same order as `groups`).
5+
6+
Return `true` *if you can do this task, and* `false` *otherwise*.
7+
8+
Note that the subarrays are **disjoint** if and only if there is no index `k` such that `nums[k]` belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
13+
<strong>Output:</strong> true
14+
<strong>Explanation:</strong> You can choose the 0th subarray as [1,-1,0,1,-1,-1,3,-2,0] and the 1st one as [1,-1,0,1,-1,-1,3,-2,0].
15+
These subarrays are disjoint as they share no common nums[k] element.
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
21+
<strong>Output:</strong> false
22+
<strong>Explanation:</strong> Note that choosing the subarrays [1,2,3,4,10,-2] and [1,2,3,4,10,-2] is incorrect because they are not in the same order as in groups.
23+
[10,-2] must come before [1,2,3,4].
24+
</pre>
25+
26+
#### Example 3:
27+
<pre>
28+
<strong>Input:</strong> groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
29+
<strong>Output:</strong> false
30+
<strong>Explanation:</strong> Note that choosing the subarrays [7,7,1,2,3,4,7,7] and [7,7,1,2,3,4,7,7] is invalid because they are not disjoint.
31+
They share a common elements nums[4] (0-indexed).
32+
</pre>
33+
34+
#### Constraints:
35+
* `groups.length == n`
36+
* <code>1 <= n <= 10<sup>3</sup></code>
37+
* <code>1 <= groups[i].length, sum(groups[i].length) <= 10<sup>3</sup></code>
38+
* <code>1 <= nums.length <= 10<sup>3</sup></code>
39+
* <code>-10<sup>7</sup> <= groups[i][j], nums[k] <= 10<sup>7</sup></code>
40+
41+
## Solutions (Rust)
42+
43+
### 1. Solution
44+
```Rust
45+
impl Solution {
46+
pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
47+
let mut i = 0;
48+
49+
for group in groups {
50+
let mut flag = true;
51+
52+
loop {
53+
if i + group.len() > nums.len() {
54+
return false;
55+
}
56+
57+
let mut j = 0;
58+
59+
while j < group.len() {
60+
if nums[i + j] != group[j] {
61+
flag = false;
62+
break;
63+
}
64+
j += 1;
65+
}
66+
67+
if flag {
68+
i += j;
69+
break;
70+
}
71+
72+
i += 1;
73+
flag = true;
74+
}
75+
}
76+
77+
true
78+
}
79+
}
80+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 1764. 通过连接另一个数组的子数组得到一个数组
2+
给你一个长度为 `n` 的二维整数数组 `groups` ,同时给你一个整数数组 `nums`
3+
4+
你是否可以从 `nums` 中选出 `n`**不相交** 的子数组,使得第 `i` 个子数组与 `groups[i]` (下标从 **0** 开始)完全相同,且如果 `i > 0` ,那么第 `(i-1)` 个子数组在 `nums` 中出现的位置在第 `i` 个子数组前面。(也就是说,这些子数组在 `nums` 中出现的顺序需要与 `groups` 顺序相同)
5+
6+
如果你可以找出这样的 `n` 个子数组,请你返回 `true` ,否则返回 `false`
7+
8+
如果不存在下标为 `k` 的元素 `nums[k]` 属于不止一个子数组,就称这些子数组是 **不相交** 的。子数组指的是原数组中连续元素组成的一个序列。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
13+
<strong>输出:</strong> true
14+
<strong>解释:</strong> 你可以分别在 nums 中选出第 0 个子数组 [1,-1,0,1,-1,-1,3,-2,0] 和第 1 个子数组 [1,-1,0,1,-1,-1,3,-2,0] 。
15+
这两个子数组是不相交的,因为它们没有任何共同的元素。
16+
</pre>
17+
18+
#### 示例 2:
19+
<pre>
20+
<strong>输入:</strong> groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
21+
<strong>输出:</strong> false
22+
<strong>解释:</strong> 选择子数组 [1,2,3,4,10,-2] 和 [1,2,3,4,10,-2] 是不正确的,因为它们出现的顺序与 groups 中顺序不同。
23+
[10,-2] 必须出现在 [1,2,3,4] 之前。
24+
</pre>
25+
26+
#### 示例 3:
27+
<pre>
28+
<strong>输入:</strong> groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
29+
<strong>输出:</strong> false
30+
<strong>解释:</strong> 选择子数组 [7,7,1,2,3,4,7,7] 和 [7,7,1,2,3,4,7,7] 是不正确的,因为它们不是不相交子数组。
31+
它们有一个共同的元素 nums[4] (下标从 0 开始)。
32+
</pre>
33+
34+
#### 提示:
35+
* `groups.length == n`
36+
* <code>1 <= n <= 10<sup>3</sup></code>
37+
* <code>1 <= groups[i].length, sum(groups[i].length) <= 10<sup>3</sup></code>
38+
* <code>1 <= nums.length <= 10<sup>3</sup></code>
39+
* <code>-10<sup>7</sup> <= groups[i][j], nums[k] <= 10<sup>7</sup></code>
40+
41+
## 题解 (Rust)
42+
43+
### 1. 题解
44+
```Rust
45+
impl Solution {
46+
pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
47+
let mut i = 0;
48+
49+
for group in groups {
50+
let mut flag = true;
51+
52+
loop {
53+
if i + group.len() > nums.len() {
54+
return false;
55+
}
56+
57+
let mut j = 0;
58+
59+
while j < group.len() {
60+
if nums[i + j] != group[j] {
61+
flag = false;
62+
break;
63+
}
64+
j += 1;
65+
}
66+
67+
if flag {
68+
i += j;
69+
break;
70+
}
71+
72+
i += 1;
73+
flag = true;
74+
}
75+
}
76+
77+
true
78+
}
79+
}
80+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
3+
let mut i = 0;
4+
5+
for group in groups {
6+
let mut flag = true;
7+
8+
loop {
9+
if i + group.len() > nums.len() {
10+
return false;
11+
}
12+
13+
let mut j = 0;
14+
15+
while j < group.len() {
16+
if nums[i + j] != group[j] {
17+
flag = false;
18+
break;
19+
}
20+
j += 1;
21+
}
22+
23+
if flag {
24+
i += j;
25+
break;
26+
}
27+
28+
i += 1;
29+
flag = true;
30+
}
31+
}
32+
33+
true
34+
}
35+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@
839839
[1758][1758l]|[Minimum Changes To Make Alternating Binary String][1758] |![rb]&nbsp;&nbsp;![rs]
840840
[1759][1759l]|[Count Number of Homogenous Substrings][1759] |![rs]
841841
[1763][1763l]|[Longest Nice Substring][1763] |![rb]&nbsp;&nbsp;![rs]
842+
[1764][1764l]|[Form Array by Concatenating Subarrays of Another Array][1764] |![rs]
842843
[1768][1768l]|[Merge Strings Alternately][1768] |![rs]
843844
[1769][1769l]|[Minimum Number of Operations to Move All Balls to Each Box][1769] |![rs]
844845
[1773][1773l]|[Count Items Matching a Rule][1773] |![rs]
@@ -1991,6 +1992,7 @@
19911992
[1758]:Problemset/1758-Minimum%20Changes%20To%20Make%20Alternating%20Binary%20String/README.md#1758-minimum-changes-to-make-alternating-binary-string
19921993
[1759]:Problemset/1759-Count%20Number%20of%20Homogenous%20Substrings/README.md#1759-count-number-of-homogenous-substrings
19931994
[1763]:Problemset/1763-Longest%20Nice%20Substring/README.md#1763-longest-nice-substring
1995+
[1764]:Problemset/1764-Form%20Array%20by%20Concatenating%20Subarrays%20of%20Another%20Array/README.md#1764-form-array-by-concatenating-subarrays-of-another-array
19941996
[1768]:Problemset/1768-Merge%20Strings%20Alternately/README.md#1768-merge-strings-alternately
19951997
[1769]:Problemset/1769-Minimum%20Number%20of%20Operations%20to%20Move%20All%20Balls%20to%20Each%20Box/README.md#1769-minimum-number-of-operations-to-move-all-balls-to-each-box
19961998
[1773]:Problemset/1773-Count%20Items%20Matching%20a%20Rule/README.md#1773-count-items-matching-a-rule
@@ -3148,6 +3150,7 @@
31483150
[1758l]:https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/
31493151
[1759l]:https://leetcode.com/problems/count-number-of-homogenous-substrings/
31503152
[1763l]:https://leetcode.com/problems/longest-nice-substring/
3153+
[1764l]:https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/
31513154
[1768l]:https://leetcode.com/problems/merge-strings-alternately/
31523155
[1769l]:https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
31533156
[1773l]:https://leetcode.com/problems/count-items-matching-a-rule/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@
839839
[1758][1758l]|[生成交替二进制字符串的最少操作数][1758] |![rb]&nbsp;&nbsp;![rs]
840840
[1759][1759l]|[统计同质子字符串的数目][1759] |![rs]
841841
[1763][1763l]|[最长的美好子字符串][1763] |![rb]&nbsp;&nbsp;![rs]
842+
[1764][1764l]|[通过连接另一个数组的子数组得到一个数组][1764] |![rs]
842843
[1768][1768l]|[交替合并字符串][1768] |![rs]
843844
[1769][1769l]|[移动所有球到每个盒子所需的最小操作数][1769] |![rs]
844845
[1773][1773l]|[统计匹配检索规则的物品数量][1773] |![rs]
@@ -1991,6 +1992,7 @@
19911992
[1758]:Problemset/1758-Minimum%20Changes%20To%20Make%20Alternating%20Binary%20String/README_CN.md#1758-生成交替二进制字符串的最少操作数
19921993
[1759]:Problemset/1759-Count%20Number%20of%20Homogenous%20Substrings/README_CN.md#1759-统计同质子字符串的数目
19931994
[1763]:Problemset/1763-Longest%20Nice%20Substring/README_CN.md#1763-最长的美好子字符串
1995+
[1764]:Problemset/1764-Form%20Array%20by%20Concatenating%20Subarrays%20of%20Another%20Array/README_CN.md#1764-通过连接另一个数组的子数组得到一个数组
19941996
[1768]:Problemset/1768-Merge%20Strings%20Alternately/README_CN.md#1768-交替合并字符串
19951997
[1769]:Problemset/1769-Minimum%20Number%20of%20Operations%20to%20Move%20All%20Balls%20to%20Each%20Box/README_CN.md#1769-移动所有球到每个盒子所需的最小操作数
19961998
[1773]:Problemset/1773-Count%20Items%20Matching%20a%20Rule/README_CN.md#1773-统计匹配检索规则的物品数量
@@ -3148,6 +3150,7 @@
31483150
[1758l]:https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/
31493151
[1759l]:https://leetcode.cn/problems/count-number-of-homogenous-substrings/
31503152
[1763l]:https://leetcode.cn/problems/longest-nice-substring/
3153+
[1764l]:https://leetcode.cn/problems/form-array-by-concatenating-subarrays-of-another-array/
31513154
[1768l]:https://leetcode.cn/problems/merge-strings-alternately/
31523155
[1769l]:https://leetcode.cn/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
31533156
[1773l]:https://leetcode.cn/problems/count-items-matching-a-rule/

0 commit comments

Comments
 (0)