Maximizing profit for given stock quotes

后端 未结 9 1919
無奈伤痛
無奈伤痛 2020-12-07 09:45

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

9条回答
  •  甜味超标
    2020-12-07 10:42

    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.
    

提交回复
热议问题