Skip to content

Commit f813947

Browse files
committed
January Leetcode challenge
1 parent d761f36 commit f813947

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Follow the link [https://gitter.im/data-structure-and-algorithms/community](http
100100
| [April '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#April-LeetCoding-Challenge--2020) | [May '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#May-LeetCoding-Challenge--2020) |
101101
| [June '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#June-LeetCoding-Challenge--2020) | [July '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#July-LeetCoding-Challenge--2020) |
102102
| [August '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#August-LeetCoding-Challenge--2020) | [September '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#September-LeetCoding-Challenge--2020) |
103-
| [November '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#November-LeetCoding-Challenge--2020) | |
103+
| [November '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#November-LeetCoding-Challenge--2020) | [January '20 Challenge](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode#January-LeetCoding-Challenge--2021) |
104104
105105
## Resources
106106

leetcode/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
| 23. | July '20 Challenge | [Search Here](#July-LeetCoding-Challenge--2020) |
3030
| 24. | August '20 Challenge | [Search Here](#August-LeetCoding-Challenge--2020) |
3131
| 25. | September '20 Challenge| [Search Here](#September-LeetCoding-Challenge--2020) |
32-
| 26. | November '20 Challenge| [Search Here](#November-LeetCoding-Challenge--2020) |
32+
| 26. | November '20 Challenge | [Search Here](#November-LeetCoding-Challenge--2020) |
33+
| 27. | January '20 Challenge | [Search Here](#January-LeetCoding-Challenge--2021) |
3334

3435
- ### **Array**
3536

@@ -96,6 +97,7 @@
9697
| 1572. | <span style="color:green">Easy</span> | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/array/matrix-diagonal-sum.py) |
9798
| 1582. | <span style="color:green">Easy</span> | [Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/array/special-positions-in-a-binary-matrix.py) |
9899
| 1588. | <span style="color:green">Easy</span> | [Sum of All Odd Length Subarrays](https://leetcode.com/problems/sum-of-all-odd-length-subarrays/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/array/sum-of-all-odd-length-subarrays.py) |
100+
| 1640. | <span style="color:green">Easy</span> | [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/array/check-array-formation-through-concatenation.py) |
99101

100102
- ### **Binary Search**
101103

@@ -427,3 +429,10 @@
427429
| :---: | :---: | :---: | :--- | :--- |
428430
| 1 | 1290. | <span style="color:green">Easy</span> | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/convert-binary-number-in-a-linked-list-to-integer.py) \| [C](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/convert-binary-number-in-a-linked-list-to-integer.c) \| [Java](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/convert-binary-number-in-a-linked-list-to-integer.java) |
429431
| 7 | 445. | <span style="color:orange">Medium</span> | [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/add-two-numbers-ii.py) |
432+
433+
- ### **January-LeetCoding-Challenge : 2021**
434+
435+
| Day | Sl No.| Level | Questions | Solution |
436+
| :---: | :---: | :---: | :--- | :--- |
437+
| 1 | 1640. | <span style="color:green">Easy</span> | [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/array/check-array-formation-through-concatenation.py) |
438+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Easy
3+
1640. [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)
4+
5+
You are given an array of distinct integers arr and an array of integer arrays pieces, where the integers in pieces are
6+
distinct. Your goal is to form arr by concatenating the arrays in pieces in any order. However, you are not allowed to
7+
reorder the integers in each array pieces[i].
8+
9+
Return true if it is possible to form the array arr from pieces. Otherwise, return false.
10+
11+
Example 1:
12+
Input: arr = [85], pieces = [[85]]
13+
Output: true
14+
15+
Example 2:
16+
Input: arr = [15,88], pieces = [[88],[15]]
17+
Output: true
18+
Explanation: Concatenate [15] then [88]
19+
20+
Example 3:
21+
Input: arr = [49,18,16], pieces = [[16,18,49]]
22+
Output: false
23+
Explanation: Even though the numbers match, we cannot reorder pieces[0].
24+
25+
Example 4:
26+
Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
27+
Output: true
28+
Explanation: Concatenate [91] then [4,64] then [78]
29+
30+
Example 5:
31+
Input: arr = [1,3,5,7], pieces = [[2,4,6,8]]
32+
Output: false
33+
34+
Constraints:
35+
1 <= pieces.length <= arr.length <= 100
36+
sum(pieces[i].length) == arr.length
37+
1 <= pieces[i].length <= arr.length
38+
1 <= arr[i], pieces[i][j] <= 100
39+
The integers in arr are distinct.
40+
The integers in pieces are distinct (i.e., If we flatten pieces in a 1D array, all the integers in this array are distinct).
41+
"""
42+
43+
# Solutions
44+
45+
46+
class Solution:
47+
"""
48+
len(arr) = m
49+
len(pieces) = n
50+
Time Complexity: O( max(m, n) )
51+
Space Complexity: O( n )
52+
"""
53+
54+
def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
55+
d = {x[0]: x for x in pieces}
56+
57+
ind = 0
58+
while ind < len(arr):
59+
value = d.get(arr[ind])
60+
if not value or arr[ind : ind + len(value)] != value:
61+
return False
62+
ind += len(value)
63+
64+
return True
65+
66+
67+
# Runtime : 28 ms, faster than 99.69% of Python3 online submissions
68+
# Memory Usage : 14.5 MB, less than 16.74% of Python3 online submissions

0 commit comments

Comments
 (0)