原题链接:https://leetcode-cn.com/problems/shopping-offers/
这题用递归来解,思路也很明显,没什么好说的,写代码的时候注意思路即可
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int cost = INT_MAX;
int len_needs = needs.size();
int len_special = special.size();
int no_special_cost = 0;
for (int t = 0; t<len_needs; t++) no_special_cost += needs[t] * price[t];
for (int i=0; i < len_special; i++){
vector<int> temp;
for (int j = 0; j<len_needs; j++){
if (needs[j] >= special[i][j]){
temp.push_back(needs[j] - special[i][j]);
}
}
if (temp.size()==len_needs){
cost = min(cost, shoppingOffers(price, special, temp) + special[i][len_needs]);
}
}
return min(cost, no_special_cost);
}