DEV Community

Discussion on: Best time to buy and sell stock

Collapse
 
vcastroi profile image
Victor Castro

problem is lowest price should not be your starting point, if in the 1st example the last price were to be 1, you would miss on 6-3=3 before it

you need 2 for cycles one inside the other, and calculate profit for each one if inner index is grater then outer index, keep track of the greatest profit.

or single for cycle and search max on the subset of items where index greater than the current one.

Collapse
 
juliansjy profile image
julian

ahhh... okok i understand. Didn't consider that initially.