forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_954.java
36 lines (34 loc) · 1.12 KB
/
_954.java
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
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class _954 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/array-of-doubled-pairs/solution/
*/
public boolean canReorderDoubled(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int i : A) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
Integer[] sorted = new Integer[A.length];
for (int i = 0; i < A.length; i++) {
sorted[i] = A[i];
}
Arrays.sort(sorted, Comparator.comparingInt(Math::abs));
for (int num : sorted) {
if (map.get(num) == 0) {
continue;
}
if (!map.containsKey(2 * num) || map.get(2 * num) <= 0) {
return false;
}
map.put(num, map.get(num) - 1);
map.put(2 * num, map.get(2 * num) - 1);
}
return true;
}
}
}