题解 | #牛群买卖计划#
牛群买卖计划
https://www.nowcoder.com/practice/3e4ae511b4a941b788da5077b08a7d07
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int n = prices.length; int[][] dp = new int[n + 1][7]; for (int i = n - 1; i >= 0; i--) { for (int j = 1; j < 7; j++) { if ((j & 1) == 1) { dp[i][j] = Math.max( dp[i + 1][j], dp[i + 1][j - 1] + prices[i] ); } else { dp[i][j] = Math.max( dp[i + 1][j], dp[i + 1][j - 1] - prices[i] ); } } } return dp[0][6]; } }