Created
June 5, 2018 14:12
-
-
Save s4553711/5c0f8f26cdeeb191ae77b0d813ce3322 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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