注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
这个算法好精妙阿。。。分享一下
profit1[i] = max profit on day i if I sell
profit2[i] = max profit on day i if I do nothing
How will those profits on day i+1 relate to profits on day i ?
1. profit1[i+1] means I must sell on day i+1, and there are 2 cases:
a. If I just sold on day i, then I have to buy again on day i and sell on day i+1
b. If I did nothing on day i, then I have to buy today and sell today
Taking both cases into account, profit1[i+1] = max(profit1[i]+prices[i+1]-prices[i], profit2[i])
2. profit2[i+1] means I do nothing on day i+1, so it will be max(profit1[i], profit2[i])
|