股票最大收益
一、问题描述
给出每天股票的价格,设计一个算法计算出最大收益。可以最多买卖两个回合。而且卖出之后才能再买。
二、样例
// 1
Input : [3, 3, 5, 0, 0, 3, 1, 4]
Output : 6
Explanation : 0-3, 1-4
// 2
Input : [1, 2, 3, 4, 5]
Output : 4
Explanation : 1-5
// 3
Input : [7, 6, 4, 3, 1]
Output : 0
Explanation : 0
三、解题思路
1、我的想法
构建一个大小为 l e n g t h ∗ ( l e n g t h + 1 ) length *(length +1) length∗(length+1)数组 d p dp dp,然后 d p [ i ] [ j ] ( i ∈ [ 0 , l e n g t h ) , j ∈ [ 0 , l e n g t h ] ) dp[i][j](i\in[0, length), j\in[0, length]) dp[i][j](i∈[0,length),j∈[0,length])表示以价格 p r i c e s [ i ] prices[i] prices[i]买入, p r i c e s [ j ] prices[j] prices[