|
| 1 | +<h2><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-using-strategy">3980. Best Time to Buy and Sell Stock using Strategy</a></h2><h3>Medium</h3><hr><p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li> |
| 5 | + <li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where: |
| 6 | + <ul> |
| 7 | + <li><code>-1</code> indicates buying one unit of the stock.</li> |
| 8 | + <li><code>0</code> indicates holding the stock.</li> |
| 9 | + <li><code>1</code> indicates selling one unit of the stock.</li> |
| 10 | + </ul> |
| 11 | + </li> |
| 12 | +</ul> |
| 13 | + |
| 14 | +<p>You are also given an <strong>even</strong> integer <code>k</code>, and may perform <strong>at most one</strong> modification to <code>strategy</code>. A modification consists of:</p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li> |
| 18 | + <li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li> |
| 19 | + <li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li> |
| 20 | +</ul> |
| 21 | + |
| 22 | +<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p> |
| 23 | + |
| 24 | +<p>Return the <strong>maximum</strong> possible profit you can achieve.</p> |
| 25 | + |
| 26 | +<p><strong>Note:</strong> There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.</p> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong class="example">Example 1:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p> |
| 33 | + |
| 34 | +<p><strong>Output:</strong> <span class="example-io">10</span></p> |
| 35 | + |
| 36 | +<p><strong>Explanation:</strong></p> |
| 37 | + |
| 38 | +<table style="border: 1px solid black;"> |
| 39 | + <thead> |
| 40 | + <tr> |
| 41 | + <th style="border: 1px solid black;">Modification</th> |
| 42 | + <th style="border: 1px solid black;">Strategy</th> |
| 43 | + <th style="border: 1px solid black;">Profit Calculation</th> |
| 44 | + <th style="border: 1px solid black;">Profit</th> |
| 45 | + </tr> |
| 46 | + </thead> |
| 47 | + <tbody> |
| 48 | + <tr> |
| 49 | + <td style="border: 1px solid black;">Original</td> |
| 50 | + <td style="border: 1px solid black;">[-1, 0, 1]</td> |
| 51 | + <td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td> |
| 52 | + <td style="border: 1px solid black;">4</td> |
| 53 | + </tr> |
| 54 | + <tr> |
| 55 | + <td style="border: 1px solid black;">Modify [0, 1]</td> |
| 56 | + <td style="border: 1px solid black;">[0, 1, 1]</td> |
| 57 | + <td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td> |
| 58 | + <td style="border: 1px solid black;">10</td> |
| 59 | + </tr> |
| 60 | + <tr> |
| 61 | + <td style="border: 1px solid black;">Modify [1, 2]</td> |
| 62 | + <td style="border: 1px solid black;">[-1, 0, 1]</td> |
| 63 | + <td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td> |
| 64 | + <td style="border: 1px solid black;">4</td> |
| 65 | + </tr> |
| 66 | + </tbody> |
| 67 | +</table> |
| 68 | + |
| 69 | +<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>.</p> |
| 70 | +</div> |
| 71 | + |
| 72 | +<p><strong class="example">Example 2:</strong></p> |
| 73 | + |
| 74 | +<div class="example-block"> |
| 75 | +<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p> |
| 76 | + |
| 77 | +<p><strong>Output:</strong> <span class="example-io">9</span></p> |
| 78 | + |
| 79 | +<p><strong>Explanation:</strong></p> |
| 80 | + |
| 81 | +<div class="example-block"> |
| 82 | +<table style="border: 1px solid black;"> |
| 83 | + <thead> |
| 84 | + <tr> |
| 85 | + <th style="border: 1px solid black;">Modification</th> |
| 86 | + <th style="border: 1px solid black;">Strategy</th> |
| 87 | + <th style="border: 1px solid black;">Profit Calculation</th> |
| 88 | + <th style="border: 1px solid black;">Profit</th> |
| 89 | + </tr> |
| 90 | + </thead> |
| 91 | + <tbody> |
| 92 | + <tr> |
| 93 | + <td style="border: 1px solid black;">Original</td> |
| 94 | + <td style="border: 1px solid black;">[1, 1, 0]</td> |
| 95 | + <td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td> |
| 96 | + <td style="border: 1px solid black;">9</td> |
| 97 | + </tr> |
| 98 | + <tr> |
| 99 | + <td style="border: 1px solid black;">Modify [0, 1]</td> |
| 100 | + <td style="border: 1px solid black;">[0, 1, 0]</td> |
| 101 | + <td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td> |
| 102 | + <td style="border: 1px solid black;">4</td> |
| 103 | + </tr> |
| 104 | + <tr> |
| 105 | + <td style="border: 1px solid black;">Modify [1, 2]</td> |
| 106 | + <td style="border: 1px solid black;">[1, 0, 1]</td> |
| 107 | + <td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td> |
| 108 | + <td style="border: 1px solid black;">8</td> |
| 109 | + </tr> |
| 110 | + </tbody> |
| 111 | +</table> |
| 112 | + |
| 113 | +<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p> |
| 114 | +</div> |
| 115 | +</div> |
| 116 | + |
| 117 | +<p> </p> |
| 118 | +<p><strong>Constraints:</strong></p> |
| 119 | + |
| 120 | +<ul> |
| 121 | + <li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li> |
| 122 | + <li><code>1 <= prices[i] <= 10<sup>5</sup></code></li> |
| 123 | + <li><code>-1 <= strategy[i] <= 1</code></li> |
| 124 | + <li><code>2 <= k <= prices.length</code></li> |
| 125 | + <li><code>k</code> is even</li> |
| 126 | +</ul> |
0 commit comments