-
Notifications
You must be signed in to change notification settings - Fork 3
/
805.cpp
29 lines (28 loc) · 1011 Bytes
/
805.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
class Solution {
public:
unordered_map<string, int> memo;
// use k element from arr[0, i] to achieve target
bool dp(vector<int>& nums, int i, int n, int target, int k) {
if (k == 0) return target == 0;
if (i == n) return false;
string key = to_string(i) + "#" + to_string(k) + "#" + to_string(target);
if (memo.find(key) != memo.end()) return memo[key];
if (nums[i] <= target) {
return memo[key] = dp(nums, i + 1, n, target - nums[i], k - 1) || dp(nums, i + 1, n, target, k);
}
else {
return memo[key] = dp(nums, i + 1, n, target, k);
}
}
bool splitArraySameAverage(vector<int>& nums) {
int n = nums.size();
int m = n / 2;
int sum = accumulate(nums.begin(), nums.end(), 0);
for (int i = 1; i <= m; ++i) {
if ((sum * i) % n == 0) {
if (dp(nums, 0, n, sum * i / n, i)) return true;
}
}
return false;
}
};