309. Buy sell stock cooldown

#include <stdlib.h>

static int max(int a, int b) { return a > b ? a : b; }

int maxProfit(int *prices, int pricesSize) {
  int n = pricesSize;
  int(*dp)[2] = calloc(n + 2, sizeof *dp); // dp[day][canBuy]

  for (int day = n - 1; day >= 0; day--) {
    // canBuy = 1
    dp[day][1] = max(-prices[day] + dp[day + 1][0], // buy
                     dp[day + 1][1]);               // skip
    // canBuy = 0 (holding)
    dp[day][0] = max(prices[day] + dp[day + 2][1], // sell + cooldown
                     dp[day + 1][0]);              // hold
  }
  int res = dp[0][1];
  free(dp);
  return res;
}
#include <algorithm>
#include <vector>

using namespace std;

class Solution {
public:
  int maxProfit(vector<int> &prices) {
    int n = prices.size();
    vector<vector<int>> dp(n + 2, vector<int>(2, 0)); // dp[day][canBuy]

    for (int day = n - 1; day >= 0; day--) {
      // canBuy = 1
      dp[day][1] = max(-prices[day] + dp[day + 1][0], // buy
                       dp[day + 1][1]);               // skip
      // canBuy = 0 (holding)
      dp[day][0] = max(prices[day] + dp[day + 2][1], // sell + cooldown
                       dp[day + 1][0]);              // hold
    }
    return dp[0][1];
  }
};
package dp

func maxProfit(prices []int) int {
    n := len(prices)
    dp := make([][2]int, n+2) // dp[day][canBuy]

    for day := n - 1; day >= 0; day-- {
        // canBuy = 1
        dp[day][1] = max(-prices[day]+dp[day+1][0], // buy
            dp[day+1][1]) // skip
        // canBuy = 0 (holding)
        dp[day][0] = max(prices[day]+dp[day+2][1], // sell + cooldown
            dp[day+1][0]) // hold
    }
    return dp[0][1]
}
class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n + 2][2]; // dp[day][canBuy]

        for (int day = n - 1; day >= 0; day--) {
            // canBuy = 1
            dp[day][1] = Math.max(-prices[day] + dp[day + 1][0], // buy
                    dp[day + 1][1]); // skip
            // canBuy = 0 (holding)
            dp[day][0] = Math.max(prices[day] + dp[day + 2][1], // sell + cooldown
                    dp[day + 1][0]); // hold
        }
        return dp[0][1];
    }
}
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  const n = prices.length;
  const dp = Array.from({ length: n + 2 }, () => [0, 0]); // dp[day][canBuy]

  for (let day = n - 1; day >= 0; day--) {
    // canBuy = 1
    dp[day][1] = Math.max(
      -prices[day] + dp[day + 1][0], // buy
      dp[day + 1][1], // skip
    );
    // canBuy = 0 (holding)
    dp[day][0] = Math.max(
      prices[day] + dp[day + 2][1], // sell + cooldown
      dp[day + 1][0], // hold
    );
  }
  return dp[0][1];
};
from typing import List


class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)
        dp = [[0, 0] for _ in range(n + 2)]  # dp[day][can_buy]

        for day in range(n - 1, -1, -1):
            # can_buy = 1
            dp[day][1] = max(
                -prices[day] + dp[day + 1][0],  # buy
                dp[day + 1][1],  # skip
            )
            # can_buy = 0 (holding)
            dp[day][0] = max(
                prices[day] + dp[day + 2][1],  # sell + cooldown
                dp[day + 1][0],  # hold
            )
        return dp[0][1]
impl Solution {
    pub fn max_profit(prices: Vec<i32>) -> i32 {
        let n = prices.len();
        let mut dp = vec![[0; 2]; n + 2]; // dp[day][can_buy]

        for day in (0..n).rev() {
            // can_buy = 1
            dp[day][1] = (-prices[day] + dp[day + 1][0]) // buy
                .max(dp[day + 1][1]); // skip
            // can_buy = 0 (holding)
            dp[day][0] = (prices[day] + dp[day + 2][1]) // sell + cooldown
                .max(dp[day + 1][0]); // hold
        }
        dp[0][1]
    }
}