-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindOriginalArray.cpp
More file actions
68 lines (67 loc) · 1.5 KB
/
findOriginalArray.cpp
File metadata and controls
68 lines (67 loc) · 1.5 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public:
vector<int> findOriginalArray(vector<int>& changed) {
if (changed.size() & 1) {
return {};
}
map<int, int> m;
for (int e: changed) {
++m[e];
}
vector<int> ans;
if (m[0]) {
if (m[0] & 1) {
return {};
}
m[0] >>= 1;
while (--m[0]) {
ans.push_back(0);
}
ans.push_back(0);
}
for (auto &[k, v] : m) {
if (!v) {
continue;
}
int e = k << 1;
if (m[e] < v) {
return {};
}
for (int i = 0; i < v; ++i) {
ans.push_back(k);
}
m[e] -= v;
v = 0;
}
return ans;
}
};
class Solution {
public:
vector<int> findOriginalArray(vector<int>& changed) {
sort(changed.begin(), changed.end());
queue<int> q;
vector<int> res;
if (changed.size() & 1) {
return {};
}
for(int e: changed){
if(q.empty()) {
q.push(e);
}
else{
if((q.front() << 1) == e){
res.push_back(q.front());
q.pop();
}
else {
q.push(e);
}
}
}
if(!q.empty()) {
return {};
}
return res;
}
};