-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitArraySameAverage.cpp
More file actions
28 lines (27 loc) · 1.02 KB
/
splitArraySameAverage.cpp
File metadata and controls
28 lines (27 loc) · 1.02 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
class Solution {
public:
bool splitArraySameAverage(vector<int>& nums) {
int n = nums.size();
if (n == 1) return false;
int s = accumulate(nums.begin(), nums.end(), 0);
for (int& v : nums) v = v * n - s;
int m = n >> 1;
unordered_set<int> vis;
for (int i = 1; i < 1 << m; ++i) {
int t = 0;
for (int j = 0; j < m; ++j) if (i >> j & 1) t += nums[j];
if (t == 0) return true;
vis.insert(t);
}
for (int i = 1; i < 1 << (n - m); ++i) {
int t = 0;
for (int j = 0; j < (n - m); ++j) if (i >> j & 1) t += nums[m + j];
if (t == 0 || (i != (1 << (n - m)) - 1 && vis.count(-t))) return true;
}
return false;
}
};
// 作者:ylb
// 链接:https://leetcode.cn/problems/split-array-with-same-average/solutions/1967149/by-lcbin-1dm3/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。