给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
[1,5,1,2,7]
9
转态转移
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if(len<2) return 0;
int[][] a = new int[2][3];
a[0][0] = -prices[0];
for(int i=1;i<len;i++)
{
a[i%2][0] = Math.max( a[(i-1)%2][1] - prices[i] , a[(i-1)%2][0] );
a[i%2][1] = a[(i-1)%2][2] ;
a[i%2][2] = Math.max(a[(i-1)%2][0] + prices[i], a[(i-1)%2][2]);
}
return Math.max( a[(len-1)%2][1] , a[(len-1)%2][2] );
}
}