1764. Form Array by Concatenating Subarrays of Another Array Medium

@problem@discussion
#Array#Greedy#String Matching



1/**
2 * [1764] Form Array by Concatenating Subarrays of Another Array
3 *
4 * You are given a 2D integer array groups of length n. You are also given an integer array nums.
5 * You are asked if you can choose n disjoint subarrays from the array nums such that the i^th subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)^th subarray appears before the i^th subarray in nums (i.e. the subarrays must be in the same order as groups).
6 * Return true if you can do this task, and false otherwise.
7 * 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.
8 *  
9 * Example 1:
10 * 
11 * Input: groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
12 * Output: true
13 * Explanation: You can choose the 0^th subarray as [1,-1,0,<u>1,-1,-1</u>,3,-2,0] and the 1^st one as [1,-1,0,1,-1,-1,<u>3,-2,0</u>].
14 * These subarrays are disjoint as they share no common nums[k] element.
15 * 
16 * Example 2:
17 * 
18 * Input: groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
19 * Output: false
20 * Explanation: Note that choosing the subarrays [<u>1,2,3,4</u>,10,-2] and [1,2,3,4,<u>10,-2</u>] is incorrect because they are not in the same order as in groups.
21 * [10,-2] must come before [1,2,3,4].
22 * 
23 * Example 3:
24 * 
25 * Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
26 * Output: false
27 * Explanation: Note that choosing the subarrays [7,7,<u>1,2,3</u>,4,7,7] and [7,7,1,2,<u>3,4</u>,7,7] is invalid because they are not disjoint.
28 * They share a common elements nums[4] (0-indexed).
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	groups.length == n
34 * 	1 <= n <= 10^3
35 * 	1 <= groups[i].length, sum(groups[i].length) <= 10^<span style="font-size: 10.8333px;">3</span>
36 * 	1 <= nums.length <= 10^3
37 * 	-10^7 <= groups[i][j], nums[k] <= 10^7
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/
43// discuss: https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
49        false
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1764() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.