贪心

难度简单

题目描述

image.png

解题思路

贪心思想,只要明天比今天股价高,那么今天买入明天卖出

Code

  1. public int maxProfit(int[] prices) {
  2. int profit = 0;
  3. for (int i = 0; i < prices.length - 1; i++) {
  4. if (prices[i + 1] > prices[i]) {
  5. profit += prices[i + 1] - prices[i];
  6. }
  7. }
  8. return profit;
  9. }