-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitArrayWithSameAverage.cpp
More file actions
31 lines (29 loc) · 1006 Bytes
/
splitArrayWithSameAverage.cpp
File metadata and controls
31 lines (29 loc) · 1006 Bytes
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
// Source: https://leetcode.com/problems/split-array-with-same-average/
// Author: Miao Zhang
// Date: 2021-03-12
class Solution {
public:
bool splitArraySameAverage(vector<int>& nums) {
int n = nums.size();
int sums = std::accumulate(nums.begin(), nums.end(), 0);
std::sort(nums.begin(), nums.end());
// sums / n = subsums / len
// sums * len = subsums * n
// sums * len / n = subsums
for (int len = 1; len <= n / 2; len++) {
if (sums * len % n == 0 && dfs(nums, 0, len, sums * len / n)) {
return true;
}
}
return false;
}
private:
bool dfs(vector<int>& nums, int start, int len, int target) {
if (len == 0) return target == 0;
for (int i = start; i < nums.size() - len + 1; i++) {
if (i != start && nums[i] == nums[i - 1]) continue;
if (dfs(nums, i + 1, len - 1, target - nums[i])) return true;
}
return false;
}
};