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

前端 未结 5 481
佛祖请我去吃肉
佛祖请我去吃肉 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

    Suppose the prices are the array P = [p_1, p_2, ..., p_n]

    Construct a new array A = [p_1, p_2 - p_1, p_3 - p_2, ..., p_n - p_{n-1}]

    i.e A[i] = p_{i+1} - p_i, taking p_0 = 0.

    Now go find the maximum sum sub-array in this.

    OR

    Find a different algorithm, and solve the maximum sub-array problem!

    The problems are equivalent.

提交回复
热议问题