Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created June 5, 2018 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s4553711/5c0f8f26cdeeb191ae77b0d813ce3322 to your computer and use it in GitHub Desktop.
Save s4553711/5c0f8f26cdeeb191ae77b0d813ce3322 to your computer and use it in GitHub Desktop.
class Solution {
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int res = inner_product(price.begin(), price.end(), needs.begin(), 0);
for (auto offer: special) {
vector<int> r = helper(offer, needs);
if (r.empty()) continue;
res = min(res, shoppingOffers(price, special, r) + offer.back());
}
return res;
}
vector<int> helper(vector<int>& offer, vector<int>& needs) {
vector<int> r(needs.size(), 0);
for(int i = 0; i < needs.size(); i++) {
if (offer[i] > needs[i]) return {};
r[i] = needs[i] - offer[i];
}
return r;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment