forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
166 lines (131 loc) · 3.98 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/// Source : https://leetcode.com/problems/split-array-with-same-average/description/
/// Author : liuyubobobo
/// Time : 2018-03-24
#include <iostream>
#include <vector>
#include <numeric>
#include <unordered_set>
#include <cassert>
#include <string>
#include <functional>
#include <utility>
using namespace std;
/// Meet in the Middle
///
/// Using a Custom Fraction class
/// The Algorithm is based on the official solution:
/// https://leetcode.com/problems/split-array-with-same-average/solution/
///
/// Time Complexity: O(2^(N/2))
/// Space Complexity: O(2^(N/2))
class Fraction{
private:
int num, denom;
public:
Fraction(int a = 0, int b = 1): num(a), denom(b){
if(num == 0)
denom = 1;
else{
if(denom < 0){
num = -num;
denom = -denom;
}
int g = gcd(abs(num), denom);
num /= g;
denom /= g;
}
}
Fraction operator-(const Fraction& another){
return Fraction(num * another.denom - another.num * denom,
denom * another.denom);
}
Fraction operator-(){
return Fraction(-num, denom);
}
Fraction operator+(const Fraction& another){
return Fraction(num * another.denom + another.num * denom,
denom * another.denom);
}
string hash_string() const{
return to_string(num) + "/" + to_string(denom);
}
private:
int gcd(int a, int b){
if(a < b)
swap(a, b);
// a > b
if(a % b == 0)
return b;
return gcd(b, a % b);
}
};
bool operator==(const Fraction& f1, const Fraction& f2) {
return f1.hash_string() == f2.hash_string();
}
bool operator!=(const Fraction& f1, const Fraction& f2) {
return !(f1 == f2);
}
// Custom Hash Function for Fraction
namespace std {
template<>
struct hash<Fraction> {
size_t operator()(Fraction const &f) const noexcept {
return std::hash<std::string>{}(f.hash_string());
}
};
}
class Solution {
public:
bool splitArraySameAverage(vector<int>& A) {
int sum = accumulate(A.begin(), A.end(), 0);
Fraction average(sum , A.size());
vector<Fraction> v;
for(int a: A)
v.push_back(Fraction(a, 1) - average);
unordered_set<Fraction> left_bag = get_sum(v, 0, A.size()/2);
if(left_bag.find(Fraction(0, 1)) != left_bag.end())
return true;
unordered_set<Fraction> right_bag = get_sum(v, A.size()/2, A.size());
if(right_bag.find(Fraction(0, 1)) != right_bag.end())
return true;
Fraction leftsum = accumulate(v.begin(), v.begin() + v.size() / 2, Fraction(0, 1));
Fraction rightsum = accumulate(v.begin() + v.size() / 2, v.end(), Fraction(0, 1));
for(Fraction num: left_bag)
if(num != leftsum && right_bag.find(-num) != right_bag.end())
return true;
return false;
}
private:
unordered_set<Fraction> get_sum(const vector<Fraction>& v, int l, int r){
unordered_set<Fraction> bag;
for(int i = l ; i < r ; i ++){
unordered_set<Fraction> new_bag;
for(Fraction e: bag)
new_bag.insert(e + v[i]);
bag.insert(new_bag.begin(), new_bag.end());
bag.insert(v[i]);
}
return bag;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<int> A1 = {1,2,3,4,5,6,7,8};
print_bool(Solution().splitArraySameAverage(A1));
// true
vector<int> A2 = {3, 1};
print_bool(Solution().splitArraySameAverage(A2));
// false
vector<int> A3 = {18, 10, 5, 3};
print_bool(Solution().splitArraySameAverage(A3));
// false
vector<int> A4 = {2,0,5,6,16,12,15,12,4};
print_bool(Solution().splitArraySameAverage(A4));
// true
vector<int> A5 = {10,29,13,53,33,48,76,70,5,5};
print_bool(Solution().splitArraySameAverage(A5));
// true
return 0;
}