forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
28 lines (27 loc) · 831 Bytes
/
Solution.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
class Solution {
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int ans = total(price, needs);
vector<int> t;
for (auto& offer : special)
{
t.clear();
for (int j = 0; j < needs.size(); ++j)
{
if (offer[j] > needs[j])
{
t.clear();
break;
}
t.push_back(needs[j] - offer[j]);
}
if (!t.empty()) ans = min(ans, offer[offer.size() - 1] + shoppingOffers(price, special, t));
}
return ans;
}
int total(vector<int>& price, vector<int>& needs) {
int s = 0;
for (int i = 0; i < price.size(); ++i) s += price[i] * needs[i];
return s;
}
};