Tap here to switch tabs
Problem
Submissions

Problem: Best Time to Buy and Sell Stock with Cooldown II

Try to solve the Best Time to Buy and Sell Stock with Cooldown II problem.

Statement

You are given an integer array prices where prices[i] is the price of a given stock on the iextthi^{ ext{th}} day, an integer array costs of the same length nn, and an integer cooldown.

You want to maximize your profit by trading the stock according to the following rules:

  • You may complete as many transactions as you like, but you cannot hold more than 11 share of stock at any time (you must sell before you can buy again).

  • If you buy the stock on day ii and sell it on day jj (where i<ji < j), the holding duration is j−ij - i days, which incurs a fee of costs[j - i]. The profit obtained from this transaction is prices[j] −- prices[i] −- costs[j - i].

  • After selling the stock on day jj, a cooldown period of cooldown days is required. This means the next purchase can only be made on or after day j+j + cooldown +1+ 1.

Return the maximum profit you can achieve.

Constraints:

  • 1≤n==1 \leq n == prices.length ≤1500\leq 1500

  • costs.length ==n== n

  • 1≤1 \leq prices[i] ≤105\leq 10^5

  • 0≤0 \leq cooldown ≤n−1\leq n - 1

  • 0≤0 \leq costs[i] ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Best Time to Buy and Sell Stock with Cooldown II

Try to solve the Best Time to Buy and Sell Stock with Cooldown II problem.

Statement

You are given an integer array prices where prices[i] is the price of a given stock on the iextthi^{ ext{th}} day, an integer array costs of the same length nn, and an integer cooldown.

You want to maximize your profit by trading the stock according to the following rules:

  • You may complete as many transactions as you like, but you cannot hold more than 11 share of stock at any time (you must sell before you can buy again).

  • If you buy the stock on day ii and sell it on day jj (where i<ji < j), the holding duration is j−ij - i days, which incurs a fee of costs[j - i]. The profit obtained from this transaction is prices[j] −- prices[i] −- costs[j - i].

  • After selling the stock on day jj, a cooldown period of cooldown days is required. This means the next purchase can only be made on or after day j+j + cooldown +1+ 1.

Return the maximum profit you can achieve.

Constraints:

  • 1≤n==1 \leq n == prices.length ≤1500\leq 1500

  • costs.length ==n== n

  • 1≤1 \leq prices[i] ≤105\leq 10^5

  • 0≤0 \leq cooldown ≤n−1\leq n - 1

  • 0≤0 \leq costs[i] ≤105\leq 10^5