If you know the future prices of a stock, what's the best time to buy and sell?

前端 未结 5 592
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 21:22

Interview Question by a financial software company for a Programmer position

Q1) Say you have an array for which the ith element is the price of a giv

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 21:53

    Your answer to question 1 was correct.

    Your answer to question 2 was not correct. To solve this problem you work backwards from the end, choosing the best option at each step. For example, given the sequence { 1, 3, 5, 4, 6 }, since 4 < 6 your last move is to sell. Since 5 > 4, the previous move to that is buy. Since 3 < 5, the move on 5 is sell. Continuing in the same way, the move on 3 is to hold and the move on 1 is to buy.

提交回复
热议问题