2969. Minimum Number of Coins for Fruits II
ArrayDynamic ProgrammingQueueHeap (Priority Queue)Monotonic Queue
Hints
Hint 1
Use DP + Fenwick tree.
Hint 2
There is another creative solution using multiset instead of Fenwick.
Hint 3
Imagine we want to calculate
dp[i] which is the answer to the problem for the first i fruits.Hint 4
If we buy
lth fruit from the set of indices: [(i + 1) / 2, (i + 1) / 2 + 1, (i + 1) / 2 + 2, ..., i - 1], then we can get fruits l + 1, l + 2, ..., i for free.Hint 5
We just need to get all the first
l - 1 fruits as well and the minimum price for that, is dp[l - 1].Hint 6
So at the index
i, we are looking for such an index l that dp[l - 1] + prices[l] is as minimum as possible.Hint 7
We can store these values in a multiset and update the values in it.
Preparing judge…