DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on

309. Leetcode Solution in java

class Solution {
  public int maxProfit(int[] prices) {
    int sell = 0;
    int hold = Integer.MIN_VALUE;
    int prev = 0;

    for (final int price : prices) {
      final int cache = sell;
      sell = Math.max(sell, hold + price);
      hold = Math.max(hold, prev - price);
      prev = cache;
    }

    return sell;
  }
}
Enter fullscreen mode Exit fullscreen mode

leetcode

challenge

Here is the link for the problem:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

Top comments (0)