-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayOfDoubledPairs954.cpp
More file actions
36 lines (35 loc) · 1.2 KB
/
Copy patharrayOfDoubledPairs954.cpp
File metadata and controls
36 lines (35 loc) · 1.2 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
class Solution {
public:
bool canReorderDoubled(vector<int>& arr) {
int end = arr.size()/2, target, j = 0;
map<int, int>count;
for (int i : arr) count[i]++;
for (auto& it: count) {
if (it.second > 0) {
target = it.first*2;
if (count[target] > 0) {
if (it.second >= count[target]) {
it.second-=count[target];
count[target] = 0;
} else {
count[target]-=it.second;
it.second = 0;
}
} else {
if (it.first%2) return false;
target = it.first/2;
if (!count.count(target) ||count[target] == 0) return false;
if (it.second >= count[target]) {
it.second-=count[target];
count[target] = 0;
} else {
count[target]-=it.second;
it.second = 0;
}
}
}
if (it.second) return false;
}
return true;
}
};