Can't solve optimization problem using greedy algorithm

19 views Asked by At

I encountered a problem during a company's online assessment a few months ago and I'm still struggling to solve it. The problem involves optimizing the purchase of items over a certain number of days given a set of pricing offers.

The function, takes 2 array as input

items_per_day = [1,5,3,4,2]

price_offers = [ [1,3,2,1], [1,1,3,4], [3,5,5,6] ]

first array indicate how many item we buy in the day (items_per_day) ex:

[1,5,3,4,2]

we buy 1 item in day 1, 5 item in day 2, 3 item in day 3 and so on

2nd array is for price (2d array)

ex:

[ [1,3,2,1], [1,1,3,4], [3,5,5,6] ]

the array is [startDay, endDay, price, numberYouCanBuy]

[1,3, 2, 1]

-> you can buy maximum 1 item that cost 2 dollar if we buy it between day 1 and day 3 (include day 1 and day 3)

[1,1,3,4]

->you can buy maximum 4 item that cost 3 dollar if we buy it in day 1

[3,5,5,6]

-> you can buy maximum 6 item that cost 5 dollar if we buy it between day 3 and day 5 (include day 1 and day 3)

you could assume we're able to find a price for every day how could we output the minimum cost from the input

if we have a items_per_day as [0,1,1], and price array that look like this

[ [2,2,2,1], [2,3,1,1], [3,3,5,1] ]

if I use greedy, I will choose offer [2,3,1,1] in day 2, and [3,3,5,1] in day 3, which total cost is 1 + 5 = 6 but we should choose offer [2,2,2,1] in day 2, and [2,3,1,1] in day 3, which total cost is 2 + 1 = 3

Thank you for help

0

There are 0 answers