-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-product-of-subsequences-with-an-alternating-sum-equal-to-k.cpp
48 lines (47 loc) · 1.68 KB
/
maximum-product-of-subsequences-with-an-alternating-sum-equal-to-k.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
// Time: O(n * k * l), l = limits
// Space: O(n * k * l)
// dp
class Solution {
public:
int maxProduct(vector<int>& nums, int k, int limit) {
const int total = accumulate(cbegin(nums), cend(nums), 0);
if (k > total || k < -total) { // optimized to speed up
return -1;
}
unordered_map<int, unordered_map<int, unordered_set<int>>> dp;
for (const auto& x : nums) {
unordered_map<int, unordered_map<int, unordered_set<int>>> new_dp;
for (const auto& [p, total_products] : dp) {
for (const auto& [total, products] : total_products) {
new_dp[p][total] = products;
}
}
new_dp[1][x].emplace(min(x, limit + 1));
for (const auto& [p, total_products] : dp) {
const int new_p = p ^ 1;
const int v = p == 0 ? x : -x;
for (const auto& [total, products] : total_products) {
const int new_total = total + v;
for (const auto& v : products) {
new_dp[new_p][new_total].emplace(min(v * x, limit + 1));
}
}
}
dp = move(new_dp);
}
int result = -1;
for (const auto& [p, total_products] : dp) {
for (const auto& [total, products] : total_products) {
if (total != k) {
continue;
}
for (const auto& v : products) {
if (v <= limit) {
result = max(result, v);
}
}
}
}
return result;
}
};