Skip to content

Commit d29ab34

Browse files
committed
add
1 parent e9e54ca commit d29ab34

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

C++/shopping-offers.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time: O(n * 2^n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
7+
return shoppingOffersHelper(price, special, needs, 0);
8+
}
9+
10+
private:
11+
int shoppingOffersHelper(const vector<int>& price, const vector<vector<int>>& special, vector<int>& needs, int i) {
12+
if (i == special.size()) {
13+
return inner_product(price.begin(), price.end(), needs.begin(), 0);
14+
}
15+
16+
int result = shoppingOffersHelper(price, special, needs, i + 1);
17+
18+
for (int j = 0; j < special[i].size() - 1; ++j) {
19+
needs[j] -= special[i][j];
20+
}
21+
if (all_of(needs.begin(), needs.end(), [](int i) { return i >= 0; })) {
22+
result = min(result, special[i].back() + shoppingOffersHelper(price, special, needs, i));
23+
}
24+
for (int j = 0; j < special[i].size() - 1; ++j) {
25+
needs[j] += special[i][j];
26+
}
27+
28+
return result;
29+
}
30+
};
31+

0 commit comments

Comments
 (0)