Skip to content

Commit c1aa2f7

Browse files
add 1640
1 parent 838b680 commit c1aa2f7

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
1112
|1637|[Widest Vertical Area Between Two Points Containing No Points](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)|[Javascript](./javascript/_1637.js)| | Medium | Sort |
1213
|1636|[Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1636.java) ||Easy|Array, Sort|
1314
|1630|[Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1630.java) ||Medium|Sort|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1640 {
4+
public static class Solution1 {
5+
public boolean canFormArray(int[] arr, int[][] pieces) {
6+
for (int[] piece : pieces) {
7+
int first = piece[0];
8+
int index = findIndex(arr, first);
9+
if (index == -1) {
10+
return false;
11+
}
12+
int i = 0;
13+
for (int j = index; i < piece.length && j < arr.length; i++, j++) {
14+
if (arr[j] != piece[i]) {
15+
return false;
16+
}
17+
}
18+
if (i != piece.length) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
25+
private int findIndex(int[] arr, int key) {
26+
for (int i = 0; i < arr.length; i++) {
27+
if (arr[i] == key) {
28+
return i;
29+
}
30+
}
31+
return -1;
32+
}
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1640;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1640Test {
10+
private static _1640.Solution1 solution1;
11+
private static int[] arr;
12+
private static int[][] pieces;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1640.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
arr = new int[]{85};
22+
pieces = new int[][]{{85}};
23+
assertEquals(true, solution1.canFormArray(arr, pieces));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
arr = new int[]{91, 4, 64, 78};
29+
pieces = new int[][]{{78}, {4, 64}, {91}};
30+
assertEquals(true, solution1.canFormArray(arr, pieces));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
arr = new int[]{49, 18, 16};
36+
pieces = new int[][]{{16, 18, 49}};
37+
assertEquals(false, solution1.canFormArray(arr, pieces));
38+
}
39+
}

0 commit comments

Comments
 (0)