|
| 1 | +<h2><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-v">3892. Best Time to Buy and Sell Stock V</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a stock in dollars on the <code>i<sup>th</sup></code> day, and an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>You are allowed to make at most <code>k</code> transactions, where each transaction can be either of the following:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li> |
| 7 | + <p><strong>Normal transaction</strong>: Buy on day <code>i</code>, then sell on a later day <code>j</code> where <code>i < j</code>. You profit <code>prices[j] - prices[i]</code>.</p> |
| 8 | + </li> |
| 9 | + <li> |
| 10 | + <p><strong>Short selling transaction</strong>: Sell on day <code>i</code>, then buy back on a later day <code>j</code> where <code>i < j</code>. You profit <code>prices[i] - prices[j]</code>.</p> |
| 11 | + </li> |
| 12 | +</ul> |
| 13 | + |
| 14 | +<p><strong>Note</strong> that you must complete each transaction before starting another. Additionally, you can't buy or sell on the same day you are selling or buying back as part of a previous transaction.</p> |
| 15 | + |
| 16 | +<p>Return the <strong>maximum</strong> total profit you can earn by making <strong>at most</strong> <code>k</code> transactions.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<div class="example-block"> |
| 22 | +<p><strong>Input:</strong> <span class="example-io">prices = [1,7,9,8,2], k = 2</span></p> |
| 23 | + |
| 24 | +<p><strong>Output:</strong> <span class="example-io">14</span></p> |
| 25 | + |
| 26 | +<p><strong>Explanation:</strong></p> |
| 27 | +We can make $14 of profit through 2 transactions: |
| 28 | + |
| 29 | +<ul> |
| 30 | + <li>A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9.</li> |
| 31 | + <li>A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2.</li> |
| 32 | +</ul> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong> <span class="example-io">prices = [12,16,19,19,8,1,19,13,9], k = 3</span></p> |
| 39 | + |
| 40 | +<p><strong>Output:</strong> <span class="example-io">36</span></p> |
| 41 | + |
| 42 | +<p><strong>Explanation:</strong></p> |
| 43 | +We can make $36 of profit through 3 transactions: |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li>A normal transaction: buy the stock on day 0 for $12 then sell it on day 2 for $19.</li> |
| 47 | + <li>A short selling transaction: sell the stock on day 3 for $19 then buy back on day 4 for $8.</li> |
| 48 | + <li>A normal transaction: buy the stock on day 5 for $1 then sell it on day 6 for $19.</li> |
| 49 | +</ul> |
| 50 | +</div> |
| 51 | + |
| 52 | +<p> </p> |
| 53 | +<p><strong>Constraints:</strong></p> |
| 54 | + |
| 55 | +<ul> |
| 56 | + <li><code>2 <= prices.length <= 10<sup>3</sup></code></li> |
| 57 | + <li><code>1 <= prices[i] <= 10<sup>9</sup></code></li> |
| 58 | + <li><code>1 <= k <= prices.length / 2</code></li> |
| 59 | +</ul> |
0 commit comments