Defining the Inputs
So basically you have a list of product pi, and each product have a list of possible coupon codes list[i]. List[i] contains all j possible the coupons applicable to pi.
array of products : p[i] = { p1, p2, p3, p4 ....}
product object :
p[i] = {
price,
coupons: list[i]
// it contains reference to all the coupons applicable to this product
}
list[1] = {c1, c2, c3, c5 ...}
list[2] = {c1, c2 ...}
list[3] = {c2, c4, c5 ...}
list[4] = {c3, c4, c5 ...}
... and so on upto list[i]
coupon object in product list :
list[i][j] = {
discount: Number,
// the amount which will be deducted from ith product if list[i][j] product is applied
name: String
// the name of the product this basically tells,
// that to which coupon in the pool of coupons does this coupon refer to.
}
Pool of all Coupons = {c1, c2 ,c2 ,c4 ,c5 ,c6, c7....}
coupon object in pool :
c[j] = {
minPriceToAvail: Number,
}
note:
we can only apply one coupon at a time.
we don't have to buy everything at once we can buy multiple times.
we won't be able to apply a coupon if the sum total of all the products we are going to buy which have this coupon applicable is less than minPriceToAvail.
For eg.s three products p1,p2,p3 with prices 2000,1000,500 and the min price to avail the coupon is 2000.
This coupon is applicable on only p2 and p3 so we can not apply it,
cause the sum total of all the products that have this coupon is only 1500
Example
p[i] = `{ shirt, laptop, book, mouse }`
// now every product have price and coupons list.
shirt = {
price: 400,
coupons: [ SALE10, DISC20, WELCOME ]
}
// each coupon here will have a discount amount and reference to pool of object
// which here for simplicity let be the same as the name of the object
shirt.coupons.SALE10 = { discount: 100, name: SALE10 }
// this means that when this coupon will be applied,
// the price of the shirt will be deducted by 100
shirt.coupons.DISC20 = { discount: 20, name: DISC20 }
shirt.coupons.WELCOME = { discount: 0, name: WELCOME }
// similarly for every product
laptop = {
price: 40000,
coupons: [
{ discount: 10000, name: DISC10 },
{ discount: 15000, name: ELECTRO },
],
}
book = {
price: 1000,
coupons: [
{ discount: 30, name: LUCKY },
{ discount: 50, name: DISC10 },
{ discount: 400, name: BOOKS },
],
}
mouse = {
price: 2000,
coupons: [
{ discount: 200, name: LUCKY },
{ discount: 1000, name: WELCOME },
],
}
allCoupons = {
SALE10: { minPriceTotal: 300 },
DISC20: { minPriceTotal: 1000 },
DISC10: { minPriceTotal: 500 },
WELCOME: { minPriceTotal: 2500 },
BOOKS: { minPriceTotal: 2000 },
LUCKY: { minPriceTotal: 500 },
ELECTRO: { minPriceTotal: 5000 },
}
Objective
The objective is to simply get the best possible combination of buying these products so as to reduce the cost to a minimum.
Just need to know the algorithm to apply here . Search Pruning is one thing that is in my mind right now but I think won't be as efficient as required.
I can only find the naive recursive solution which will run in O( product of n(li) ), i.e. In Order of Product of number of coupons in each product.
Like in the example below the product will be 3*2*3*2 = 36