Skip to content

Commit 176c705

Browse files
committed
0954 added.
1 parent b8bf633 commit 176c705

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(B main.cpp)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// Source : https://leetcode.com/problems/array-of-doubled-pairs/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-12-12
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <unordered_map>
8+
9+
using namespace std;
10+
11+
12+
/// Greedy, from large absolute value to small absolute value
13+
/// Time Complexity: O(nlogn)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
bool canReorderDoubled(vector<int>& A) {
18+
19+
unordered_map<int, int> freq;
20+
for(int a: A)
21+
freq[a] ++;
22+
23+
sort(A.begin(), A.end(), [](int a, int b){return abs(a) < abs(b);});
24+
// for(int a: A)
25+
// cout << a << " ";
26+
// cout << endl;
27+
28+
for(int a: A){
29+
if(freq[a]){
30+
if(!freq[2 * a])
31+
return false;
32+
freq[a] --;
33+
if(!freq[2 * a])
34+
return false;
35+
freq[2 * a] --;
36+
}
37+
}
38+
return true;
39+
}
40+
};
41+
42+
43+
int main() {
44+
45+
vector<int> A1 = {4, -2, 2, -4};
46+
cout << Solution().canReorderDoubled(A1) << endl;
47+
48+
return 0;
49+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,6 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
613613
| 951 | [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [solution](https://leetcode.com/problems/flip-equivalent-binary-trees/solution/) | [C++](0951-Flip-Equivalent-Binary-Trees/cpp-0951/) | | |
614614
| 952 | [Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/) | [solution](https://leetcode.com/problems/largest-component-size-by-common-factor/solution/) | [C++](0952-Largest-Component-Size-by-Common-Factor/cpp-0952/) | | |
615615
| 953 | [isAlienSorted](https://leetcode.com/problems/verifying-an-alien-dictionary/) | [solution](https://leetcode.com/problems/verifying-an-alien-dictionary/solution/) | [C++](0953-isAlienSorted/cpp-0953/) | | |
616-
| | | | | | |
616+
| 954 | [Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/) | [solution](https://leetcode.com/problems/array-of-doubled-pairs/solution/) | [C++](0954-Array-of-Doubled-Pairs/cpp-0954/) | | |
617617
| | | | | | |
618618
| | | | | | |

0 commit comments

Comments
 (0)