Shopping Offers
Thoughts
Code
class Solution {
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
int result = Integer.MAX_VALUE;
// apply each offter to the needs, and recurse
for(List<Integer> offer : special) {
boolean valid = true;
for (int j = 0; j < needs.size(); j++) {
int remain = needs.get(j) - offer.get(j);
needs.set(j, remain);
if (valid && remain < 0) {
valid = false;
}
}
if (valid) {
result = Math.min(result, shoppingOffers(price, special, needs) + offer.get(needs.size()));
}
for (int j = 0; j < needs.size(); j++) {
int remain = needs.get(j) + offer.get(j);
needs.set(j, remain);
}
}
int nonOfferPrice = 0;
for (int i = 0; i < needs.size(); i++) {
nonOfferPrice += price.get(i) * needs.get(i);
}
return Math.min(result, nonOfferPrice);
}
}Analysis
Last updated