forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
48 lines (38 loc) · 1015 Bytes
/
main2.cpp
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
/// Source : https://leetcode.com/problems/array-partition-i/solution/
/// Author : liuyubobobo
/// Time : 2018-06-04
#include <iostream>
#include <vector>
using namespace std;
/// Using HashMap
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int hash[20001];
memset(hash, 0, sizeof(hash));
for(int num: nums)
hash[num + 10000] ++;
int sum = 0;
bool minus = false;
for(int i = 0 ; i <= 20000 ; i ++)
if(hash[i]){
if(minus){
hash[i] --;
minus = false;
}
sum += hash[i] / 2 * (i - 10000);
if(hash[i] % 2){
sum += (i - 10000);
minus = true;
}
}
return sum;
}
};
int main() {
vector<int> nums = {1, 4, 3, 2};
cout << Solution().arrayPairSum(nums) << endl;
return 0;
}