一、题目内容

image.png

二、题解

解法1:

思路

代码

  1. public class Solution {
  2. public int maxProfit (int[] prices) {
  3. // write code here
  4. if(prices == null || prices.length<2){
  5. return 0;
  6. }
  7. int len = prices.length;
  8. int[][] dp = new int[len][2];
  9. dp[0][1] = -prices[0];
  10. dp[0][0] = 0;
  11. for(int i = 1;i<len;i++){
  12. dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
  13. dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);
  14. }
  15. return dp[len-1][0];
  16. }
  17. }