public int maxProfit(int[] prices) {

    int max = 0;
    int currtMax = 0;

    for (int i = 1 ; i < prices.length ; i++) {
      int diff = prices[i] - prices[i - 1];
      if ((diff + currtMax) < 0) {
        currtMax = 0;
      } else {
        currtMax += diff;
      }

      if (currtMax > max) {
        max = currtMax;
      }
    }

    return max;
  }