Skip to content

Commit 4b43700

Browse files
add 954
1 parent 0d96329 commit 4b43700

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ _If you like this project, please leave me a star._ ★
8383
|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | O(hlogn) | O(n) | |Medium| Hash Table, String
8484
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion|
8585
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
86+
|954|[Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_954.java) | | | |Medium|
8687
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
8788
|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_951.java) | O(n) | O(h) | |Medium| Tree, DFS, recursion|
8889
|950|[Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_950.java) | O(nlogn) | O(n) | |Medium|
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* 954. Array of Doubled Pairs
9+
*
10+
* Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
11+
*
12+
* Example 1:
13+
* Input: [3,1,3,6]
14+
* Output: false
15+
*
16+
* Example 2:
17+
* Input: [2,1,2,6]
18+
* Output: false
19+
*
20+
* Example 3:
21+
* Input: [4,-2,2,-4]
22+
* Output: true
23+
* Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
24+
*
25+
* Example 4:
26+
* Input: [1,2,4,16,8,4]
27+
* Output: false
28+
*
29+
* Note:
30+
* 0 <= A.length <= 30000
31+
* A.length is even
32+
* -100000 <= A[i] <= 100000
33+
* */
34+
public class _954 {
35+
public static class Solution1 {
36+
public boolean canReorderDoubled(int[] A) {
37+
for (int i = 0; i < A.length; i++) {
38+
if (A[i] < 0) {
39+
A[i] = -A[i];
40+
}
41+
}
42+
Arrays.sort(A);
43+
Map<Integer, Integer> map = new HashMap<>();
44+
for (int num : A) {
45+
map.put(num, map.getOrDefault(num, 0) + 1);
46+
}
47+
for (int num : A) {
48+
if (num != 0) {
49+
if (map.get(num) < 0) {
50+
return false;
51+
} else if (map.get(num) == 0) {
52+
continue;
53+
} else {
54+
int count = map.get(num);
55+
map.put(num, map.get(num) - count);
56+
int doubleNum = num * 2;
57+
if (!map.containsKey(doubleNum)) {
58+
return false;
59+
} else {
60+
map.put(doubleNum, map.get(doubleNum) - count);
61+
}
62+
}
63+
} else if (map.get(num) % 2 != 0) {
64+
return false;
65+
}
66+
}
67+
return true;
68+
}
69+
}
70+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._954;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _954Test {
10+
private static _954.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _954.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{3, 1, 3, 6};
21+
assertEquals(false, solution1.canReorderDoubled(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[]{2, 1, 2, 6};
27+
assertEquals(false, solution1.canReorderDoubled(A));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
A = new int[]{4, -2, 2, -4};
33+
assertEquals(true, solution1.canReorderDoubled(A));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
A = new int[]{1, 2, 4, 16, 8, 4};
39+
assertEquals(false, solution1.canReorderDoubled(A));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
A = new int[]{1, 2, 4, 8};
45+
assertEquals(true, solution1.canReorderDoubled(A));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
A = new int[]{10, 20, 40, 80};
51+
assertEquals(true, solution1.canReorderDoubled(A));
52+
}
53+
54+
@Test
55+
public void test7() {
56+
A = new int[]{0, 0};
57+
assertEquals(true, solution1.canReorderDoubled(A));
58+
}
59+
}

0 commit comments

Comments
 (0)