The recurrence relation of the following problem can be defined as f(n)=a[n]+min( f(n-1),f(n-2),...,f(n-k)), where a[i] is the value of petrol per litre.
We can use the memorization technique to solve the following, to get minimum of last k elements we can maintain a heap.
My approach was this:
for every planet a[i], we need to go to that planet from a[i+1] to a[i+k+1] with the minimum weight/cost “to the end”
to calculate the weight “to the end” we start from last and memoize the results
let n be 8 and k be 3 then the dependency would look like this
so we start from the last and then maintain a priority_queue/multiset for the next k elements and remove them once we are past k elements O(NlgK )
(OR)
a deque for O(N) my code here