-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindOriginalArrayFromDoubledArray.java
53 lines (51 loc) · 1.71 KB
/
FindOriginalArrayFromDoubledArray.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*https://leetcode.com/problems/find-original-array-from-doubled-array/*/
class Solution {
public int[] findOriginalArray(int[] changed) {
if (changed.length%2 != 0) return new int[0];
Arrays.sort(changed);
int n = changed.length;
TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>();
for (int num : changed) map.put(num,map.getOrDefault(num,0)+1);
int[] result = new int[n/2];
int index = 0;
while (!map.isEmpty())
{
int num = (Integer)map.firstKey();
if (!map.containsKey(num*2)) return new int[0];
result[index++] = num;
map.put(num,map.get(num)-1);
if (map.get(num) == 0) map.remove(num);
num *= 2;
if (!map.containsKey(num)) return new int[0];
map.put(num,map.get(num)-1);
if (map.get(num) == 0) map.remove(num);
}
return result;
}
}
class Solution {
public int[] findOriginalArray(int[] changed) {
if (changed.length%2 != 0) return new int[0];
int n = changed.length;
int max = 0;
for (int num : changed)
if (num > max)
max = num;
if (max%2 != 0) return new int[0];
int[] map = new int[max+1];
for (int num : changed) ++map[num];
int[] result = new int[n/2];
int index = 0;
for (int i = 0; i <= max; ++i)
{
if (map[i] == 0) continue;
if (2*i > max || map[2*i] == 0) return new int[0];
result[index++] = i;
--map[i];
if (map[2*i] == 0) return new int[0];
--map[2*i];
if (map[i] > 0) --i;
}
return result;
}
}