I was asked this question while interviewing for a startup and saw this again in the recent contest at
Code Sprint:systems
**The question :
You are
I just solved that problem in a contest site. I think I got a simpler algorithm than the accepted answer.
1. smax = maximum stock price from the list
2. then find the profit by assuming you have bought all the stocks till smax
and you sell it at the price of smax
3. then check if smax is the last element of the stock price list
if yes then return profit as answer,
if no
then make a new list containing stock prices after smax to the last stock price
and repeat steps 1-3 and keep adding profit of each iteration to get the final profit.