You are given a 2D integer array groups of length n. You are also given an integer array nums.
You are asked if you can choose ndisjoint subarrays from the array
nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray appears before the ith subarray in nums (i.e. the subarrays must be in the same order as groups).
Return trueif you can do this task, andfalseotherwise.
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.
Input: groups =[[1,-1,-1],[3,-2,0]], nums =[1,-1,0,1,-1,-1,3,-2,0]Output: trueExplanation: 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.
Input: groups =[[10,-2],[1,2,3,4]], nums =[1,2,3,4,10,-2]Output: falseExplanation: 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].
Input: groups =[[1,2,3],[3,4]], nums =[7,7,1,2,3,4,7,7]Output: falseExplanation: 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).
We want to match each group in order as a contiguous subarray in nums, ensuring that the matches are disjoint and in order. We can use a pointer to scan through nums and try to match each group one by one.
classSolution {
publicbooleancanChoose(int[][] groups, int[] nums) {
int i = 0;
for (int[] g : groups) {
boolean found =false;
while (i + g.length<= nums.length) {
boolean match =true;
for (int j = 0; j < g.length; j++) {
if (nums[i + j]!= g[j]) {
match =false;
break;
}
}
if (match) {
i += g.length;
found =true;
break;
} else {
i++;
}
}
if (!found) returnfalse;
}
returntrue;
}
}
classSolution {
funcanChoose(groups: Array<IntArray>, nums: IntArray): Boolean {
var i = 0for (g in groups) {
var found = falsewhile (i + g.size <= nums.size) {
var match = truefor (j in g.indices) {
if (nums[i + j] != g[j]) {
match = falsebreak }
}
if (match) {
i += g.size
found = truebreak } else {
i++ }
}
if (!found) returnfalse }
returntrue }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcanChoose(self, groups: list[list[int]], nums: list[int]) -> bool:
i =0for g in groups:
found =Falsewhile i + len(g) <= len(nums):
if nums[i:i+len(g)] == g:
i += len(g)
found =Truebreak i +=1ifnot found:
returnFalsereturnTrue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfncan_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
letmut i =0;
for g in groups {
letmut found =false;
while i + g.len() <= nums.len() {
if nums[i..i+g.len()] == g[..] {
i += g.len();
found =true;
break;
}
i +=1;
}
if!found { returnfalse; }
}
true }
}
⏰ Time complexity: O(m + n), where m is the total length of all groups and n is the length of nums, since each element is visited at most once per group.
🧺 Space complexity: O(1), as only pointers and counters are used.