Please explain the following question with an example??
Why should we think of the problem in terms of dp here??
Please explain the following question with an example??
First, we must buy products in order. So, to buy product i, we only need to know where we bought product i-1.
So, the state is a pair (product to buy, last shop used)
Btw, did you check the editorial?
For applying dp, think the very basic way: we need two properties to be satisfied here. Optimal Substructure, if we consider buying ith item from jth shop, then the choice of j here can be the one same j from which we bought (i-1)th item or any other. This means solution of problem (i,j) depends on solution of subproblems (i-1,j) or (i-1,shop) where shop denotes any other shop which has minimum cost for selling (i-1)th item. And similarly overlapping property is also satisfied here. Thus, we can apply dp here.
So, for buying i’th item, we have two possibilities. Let us assume that we bought (i-1)th item from from some shop k(1<=k<=m). Then,
Hope this clears…
Thanks… got it