Maximum profit array c++ multiple sell -
Maximum profit array c++ multiple sell -
given array of size n
represents prices of object on n
days . each day have either purchase single object or can sell amount of objects have earn money.assume can purchase object. , can sell if have objects. maximize total turn a profit have take particular day selling , buying.
for illustration ..n=10 arr={5,0,9,2,9,0,2,7,4,7}
, lets total no of objects have n=0;
, total turn a profit p= 0
on day 1 don't have object in case have purchase 1 .. n=1 , p=-5
, on day 2 can purchase sec 1 no amount spend n=2
, p=-5
on day 3 can sell both objects , earn money n=0
, p=13
can proceed farther , maximum turn a profit earned 35
please help me question. have seen problems have take days sell , purchase ..but seem help me because here have either purchase or sell each day ..please give me thought how proceed
if maintain average of price, purchase when below average, , sell when above average.
you have maintain total of each day's price, 5 day 1 , two, 14 day 3 , on.
then split number of days passed far, 14/3 = 4.66
if day greater average , have objects, sell. otherwise, buy.
hope helps!
c++ arrays algorithm max
Comments
Post a Comment