Class Solution
- java.lang.Object
-
- g1701_1800.s1764_form_array_by_concatenating_subarrays_of_another_array.Solution
-
public class Solution extends Object
1764 - Form Array by Concatenating Subarrays of Another Array.Medium
You are given a 2D integer array
groupsof lengthn. You are also given an integer arraynums.You are asked if you can choose
ndisjoint subarrays from the arraynumssuch that theithsubarray is equal togroups[i]( 0-indexed ), and ifi > 0, the(i-1)thsubarray appears before theithsubarray innums(i.e. the subarrays must be in the same order asgroups).Return
trueif you can do this task, andfalseotherwise.Note that the subarrays are disjoint if and only if there is no index
ksuch thatnums[k]belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.Example 1:
Input: groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
Output: true
Explanation: 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 ]. These subarrays are disjoint as they share no common nums[k] element.
Example 2:
Input: groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
Output: false
Explanation: 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. [10,-2] must come before [1,2,3,4].
Example 3:
Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
Output: false
Explanation: 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. They share a common elements nums[4] (0-indexed).
Constraints:
groups.length == n1 <= n <= 1031 <= groups[i].length, sum(groups[i].length) <= 1031 <= nums.length <= 103-107 <= groups[i][j], nums[k] <= 107
-
-
Constructor Summary
Constructors Constructor Description Solution()
-