Given an array which have samples of stock price at different timestamps.
You have to find best timings when we can buy and sell and make maximum benifit by it.
No need to say u can sell only after you buy.
Its simple DP problem, just need to form a equation
MaxGain(i) = Max(MaxGain(i-1), DIff(i,min))
Min is the index of element having min value till now. Diff will return the value(i)-value(min).
Hope its clear.
You have to find best timings when we can buy and sell and make maximum benifit by it.
No need to say u can sell only after you buy.
Its simple DP problem, just need to form a equation
MaxGain(i) = Max(MaxGain(i-1), DIff(i,min))
Min is the index of element having min value till now. Diff will return the value(i)-value(min).
Hope its clear.
Comments
Post a Comment