309. Buy sell stock cooldown
309_solution1_memo.c
#include <stdlib.h>
#include <string.h>
static int max(int a, int b) { return a > b ? a : b; }
static int dpf(int day, int holding, int (*memo)[2], int *prices, int n) {
if (day >= n)
return 0;
if (memo[day][holding] != -1)
return memo[day][holding];
int profit = dpf(day + 1, holding, memo, prices, n);
if (holding == 0) {
profit = max(profit, dpf(day + 1, 1, memo, prices, n) - prices[day]);
} else {
profit = max(profit, dpf(day + 2, 0, memo, prices, n) + prices[day]);
}
memo[day][holding] = profit;
return profit;
}
int maxProfit(int *prices, int pricesSize) {
int n = pricesSize;
int(*memo)[2] = malloc(n * sizeof *memo);
memset(memo, -1, n * sizeof *memo);
int res = dpf(0, 0, memo, prices, n);
free(memo);
return res;
}
309_solution2_tab.c
#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;
}
309_solution1_memo.cpp
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
int dpf(int day, int holding, vector<vector<int>> &memo,
vector<int> &prices) {
if (day >= prices.size())
return 0;
if (memo[day][holding] != -1)
return memo[day][holding];
int profit = dpf(day + 1, holding, memo, prices);
if (holding == 0) {
profit = max(profit, dpf(day + 1, 1, memo, prices) - prices[day]);
} else {
profit = max(profit, dpf(day + 2, 0, memo, prices) + prices[day]);
}
memo[day][holding] = profit;
return profit;
}
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
vector<vector<int>> memo(n, vector<int>(2, -1));
return dpf(0, 0, memo, prices);
}
};
309_solution2_tab.cpp
#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];
}
};
309_solution1_memo.go
package dp
func maxProfit(prices []int) int {
n := len(prices)
memo := make([][2]int, n)
for i := range memo {
memo[i] = [2]int{-1, -1}
}
var dpf func(day, holding int) int
dpf = func(day, holding int) int {
if day >= n {
return 0
}
if memo[day][holding] != -1 {
return memo[day][holding]
}
profit := dpf(day+1, holding)
if holding == 0 {
profit = max(profit, dpf(day+1, 1)-prices[day])
} else {
profit = max(profit, dpf(day+2, 0)+prices[day])
}
memo[day][holding] = profit
return profit
}
return dpf(0, 0)
}
309_solution2_tab.go
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]
}
309_solution1_memo.java
import java.util.Arrays;
class Solution {
private int dpf(int day, int holding, int[][] memo, int[] prices) {
if (day >= prices.length)
return 0;
if (memo[day][holding] != -1)
return memo[day][holding];
int profit = dpf(day + 1, holding, memo, prices);
if (holding == 0) {
profit = Math.max(profit, dpf(day + 1, 1, memo, prices) - prices[day]);
} else {
profit = Math.max(profit, dpf(day + 2, 0, memo, prices) + prices[day]);
}
memo[day][holding] = profit;
return profit;
}
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] memo = new int[n][2];
for (int[] row : memo)
Arrays.fill(row, -1);
return dpf(0, 0, memo, prices);
}
}
309_solution2_tab.java
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];
}
}
309_solution1_memo.js
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
const n = prices.length;
const memo = Array.from({ length: n }, () => [-1, -1]);
const dpf = (day, holding) => {
if (day >= n) return 0;
if (memo[day][holding] !== -1) return memo[day][holding];
let profit = dpf(day + 1, holding);
if (holding === 0) {
profit = Math.max(profit, dpf(day + 1, 1) - prices[day]);
} else {
profit = Math.max(profit, dpf(day + 2, 0) + prices[day]);
}
memo[day][holding] = profit;
return profit;
};
return dpf(0, 0);
};
309_solution2_tab.js
/**
* @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];
};
309_solution1_memo.py
from typing import List
class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
memo = [[-1, -1] for _ in range(n)]
def dpf(day: int, holding: int) -> int:
if day >= n:
return 0
if memo[day][holding] != -1:
return memo[day][holding]
profit = dpf(day + 1, holding)
if holding == 0:
profit = max(profit, dpf(day + 1, 1) - prices[day])
else:
profit = max(profit, dpf(day + 2, 0) + prices[day])
memo[day][holding] = profit
return profit
return dpf(0, 0)
309_solution2_tab.py
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]
309_solution1_memo.rs
impl Solution {
fn dpf(day: usize, holding: usize, memo: &mut Vec<[i32; 2]>, prices: &[i32]) -> i32 {
if day >= prices.len() {
return 0;
}
if memo[day][holding] != -1 {
return memo[day][holding];
}
let mut profit = Self::dpf(day + 1, holding, memo, prices);
if holding == 0 {
profit = profit.max(Self::dpf(day + 1, 1, memo, prices) - prices[day]);
} else {
profit = profit.max(Self::dpf(day + 2, 0, memo, prices) + prices[day]);
}
memo[day][holding] = profit;
profit
}
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut memo = vec![[-1; 2]; prices.len()];
Self::dpf(0, 0, &mut memo, &prices)
}
}
309_solution2_tab.rs
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]
}
}