LeetCode 122. 买股票的最佳时机 II (状态自动机)
原题链接
中等
作者:
JustDoIt11
,
2024-03-16 20:13:27
,
所有人可见
,
阅读 11
/**
* @Title: lec122
* @Author 李浩天
* @Date 2024/3/16 下午7:38
* @description:
*/
public class lec122 {
// 状态自动机
public int maxProfit(int[] prices) {
int[][] f = new int[prices.length][2];
f[0][1] = -prices[0];
f[0][0] = 0;
for (int i = 1; i < prices.length; i ++ ) {
f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - prices[i]);
f[i][0] = Math.max(f[i - 1][0], f[i - 1][1] + prices[i]);
}
return Math.max(f[prices.length - 1][0], f[prices.length - 1][1]);
}
}