|
| 1 | +""" |
| 2 | + You are given an array of distinct integers arr and an array of integer |
| 3 | + arrays pieces, where the integers in pieces are distinct. Your goal is to |
| 4 | + form arr by concatenating the arrays in pieces in any order. However, you |
| 5 | + are not allowed to reorder the integers in each array pieces[i]. |
| 6 | +
|
| 7 | + Return true if it is possible to form the array arr from pieces. |
| 8 | + Otherwise, return false. |
| 9 | +
|
| 10 | + Example: |
| 11 | + Input: arr = [85], pieces = [[85]] |
| 12 | + Output: true |
| 13 | +
|
| 14 | + Example: |
| 15 | + Input: arr = [15,88], pieces = [[88],[15]] |
| 16 | + Output: true |
| 17 | + Explanation: Concatenate [15] then [88] |
| 18 | +
|
| 19 | + Example: |
| 20 | + Input: arr = [49,18,16], pieces = [[16,18,49]] |
| 21 | + Output: false |
| 22 | + Explanation: Even though the numbers match, we cannot reorder pieces[0]. |
| 23 | +
|
| 24 | + Example: |
| 25 | + Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]] |
| 26 | + Output: true |
| 27 | + Explanation: Concatenate [91] then [4,64] then [78] |
| 28 | +
|
| 29 | + Example: |
| 30 | + Input: arr = [1,3,5,7], pieces = [[2,4,6,8]] |
| 31 | + Output: false |
| 32 | +
|
| 33 | + Constraints: |
| 34 | + - 1 <= pieces.length <= arr.length <= 100 |
| 35 | + - sum(pieces[i].length) == arr.length |
| 36 | + - 1 <= pieces[i].length <= arr.length |
| 37 | + - 1 <= arr[i], pieces[i][j] <= 100 |
| 38 | + - The integers in arr are distinct. |
| 39 | + - The integers in pieces are distinct (i.e., If we flatten pieces in |
| 40 | + a 1D array, all the integers in this array are distinct). |
| 41 | +""" |
| 42 | +#Difficulty: Easy |
| 43 | +#82 / 82 test cases passed. |
| 44 | +#Runtime: 40 ms |
| 45 | +#Memory Usage: 14 MB |
| 46 | + |
| 47 | +#Runtime: 40 ms, faster than 86.23% of Python3 online submissions for Check Array Formation Through Concatenation. |
| 48 | +#Memory Usage: 14 MB, less than 100.00% of Python3 online submissions for Check Array Formation Through Concatenation. |
| 49 | + |
| 50 | +class Solution: |
| 51 | + def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: |
| 52 | + array = [None] * len(arr) |
| 53 | + for piece in pieces: |
| 54 | + lenght = len(piece) |
| 55 | + if piece[0] in arr: |
| 56 | + i = arr.index(piece[0]) |
| 57 | + if arr[i:i+lenght] == piece: |
| 58 | + for p in piece: |
| 59 | + array[i] = p |
| 60 | + i += 1 |
| 61 | + return None not in array |
0 commit comments