-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy path1640_Check_Array_Formation_Through_Concatenation
More file actions
77 lines (63 loc) · 1.81 KB
/
1640_Check_Array_Formation_Through_Concatenation
File metadata and controls
77 lines (63 loc) · 1.81 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Leetcode 1640: Check Array Formation Through Concatenation
Detailed video Explanation: https://youtu.be/1Q4N98UQnuc
==========================================================
C++:
----
class Solution {
public:
bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
unordered_map<int, vector<int>> _map;
for(auto p: pieces)
_map.insert({p[0], p});
int idx = 0;
while(idx < arr.size()){
if(_map.count(arr[idx])){
auto piece = _map[arr[idx]];
for(int p: piece){
if(p != arr[idx]) return false;
else idx++;
}
} else
return false;
}
return true;
}
};
Java:
-----
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
Map<Integer, int[]> _map = new HashMap<>();
for(int[] p: pieces)
_map.put(p[0], p);
int idx = 0;
while(idx < arr.length){
if(_map.containsKey(arr[idx])){
int[] piece = _map.get(arr[idx]);
for(int p: piece){
if(p != arr[idx]) return false;
else idx++;
}
} else
return false;
}
return true;
}
}
Python3:
--------
class Solution:
def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
_map = {}
for p in pieces:
_map[p[0]] = p
idx = 0
while idx < len(arr):
if arr[idx] in _map:
piece = _map[arr[idx]]
for p in piece:
if p != arr[idx]: return False
else: idx += 1
else:
return False
return True