I don’t really understand why we took 2 arrays left and right to solve this question and what was the reasoning behind it.
How does buying 2 stocks relate to a bidirectional dp question? The video is a bit unclear on this and it does not seem intuitive. I can code it and the answer is correct but the reasoning behind it is still unclear
Buy and Sell Stock part 3
hello @Lakshya-Pawa-10213947019988824
the idea is simple .
we need to make two transactions right?
so what we are doing we are dividing our array into two parts. in first part we making one transation and in second part we are making second transactions .
now we can divide array into two parts from various indexes(from 0 to n-1) .
from all such possible partition one partion will exist which will maximisee our prifit.
to avoid computation cost of calculating transaction in each partition we are using these two arrays(left / right)
left[i] will store maximum profit from first transaction when (0 to i is considered )
right[i]will store maximum profit from second transaction when (i,n-1 is considered).
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.