-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path638.cpp
More file actions
39 lines (37 loc) · 1.16 KB
/
638.cpp
File metadata and controls
39 lines (37 loc) · 1.16 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
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
int need2state(vector<int>& needs) {
int state = 0;
for (auto& need : needs) {
state += need;
state <<= 4;
}
return state;
}
unordered_map<int, int> memo;
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int res = 0;
int n = price.size();
int state = need2state(needs);
if (memo.find(state) != memo.end()) return memo[state];
for (int i = 0; i < n; ++i) {
res += price[i] * needs[i];
}
for (auto& order : special) {
bool isValid = true;
for (int i = 0; i < n; ++i) {
if (order[i] > needs[i]) isValid = false;
}
if (isValid) {
for (int i = 0; i < n; ++i) {
needs[i] -= order[i];
}
res = min(res, shoppingOffers(price, special, needs) + order.back());
for (int i = 0; i < n; ++i) {
needs[i] += order[i];
}
}
}
return memo[state] = res;
}
};