-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1764-FormArraybyConcatenatingSubarraysofAnotherArray.py
More file actions
31 lines (25 loc) · 1.26 KB
/
1764-FormArraybyConcatenatingSubarraysofAnotherArray.py
File metadata and controls
31 lines (25 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 1764. Form Array by Concatenating Subarrays of Another Array
# 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 n disjoint 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 true if you can do this task, and false otherwise.
# 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.
class Solution:
def canChoose(self, groups: List[List[int]], nums: List[int]) -> bool:
i = 0
g = False
while i < len(nums):
if not g:
if len(groups) == 0:
return True
else:
g = groups.pop(0)
if g == nums[i : i + len(g)]:
print(nums[i : i + len(g)])
i += len(g)
print(nums[i:])
g = False
else:
i += 1
if len(groups) == 0 and g == False:
return True
return False