[leetcode]638. Shopping Offers
Analysis
fighting!!!—— [每天刷题并不难0.0]
In LeetCode Store, there are some kinds of items to sell. Each item has a price.
However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.
You are given the each item’s price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers.
Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.
You could use any of special offers as many times as you want.
实不相瞒,一般这种题目话特别多的,我都直接看example~
DFS解决~
Implement
class Solution {
public:
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
int len = price.size();
int res = 0;
for(int i=0; i<len; i++)
res += price[i]*needs[i];
for(int i=0; i<special.size(); i++){
bool valid = true;
for(int j=0; j<len; j++){
if(needs[j]-special[i][j] < 0)
valid = false;
needs[j] -= special[i][j];
}
if(valid)
res = min(res, special[i][len]+shoppingOffers(price, special, needs));
for(int j=0; j<len; j++)
needs[j] += special[i][j];
}
return res;
}
};