|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Medium |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3652.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20using%20Strategy/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3652. Best Time to Buy and Sell Stock using Strategy](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-using-strategy) |
| 10 | + |
| 11 | +[中文文档](/solution/3600-3699/3652.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20using%20Strategy/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li> |
| 21 | + <li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where: |
| 22 | + <ul> |
| 23 | + <li><code>-1</code> indicates buying one unit of the stock.</li> |
| 24 | + <li><code>0</code> indicates holding the stock.</li> |
| 25 | + <li><code>1</code> indicates selling one unit of the stock.</li> |
| 26 | + </ul> |
| 27 | + </li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<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> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li> |
| 34 | + <li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li> |
| 35 | + <li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li> |
| 36 | +</ul> |
| 37 | + |
| 38 | +<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p> |
| 39 | + |
| 40 | +<p>Return the <strong>maximum</strong> possible profit you can achieve.</p> |
| 41 | + |
| 42 | +<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> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong class="example">Example 1:</strong></p> |
| 46 | + |
| 47 | +<div class="example-block"> |
| 48 | +<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p> |
| 49 | + |
| 50 | +<p><strong>Output:</strong> <span class="example-io">10</span></p> |
| 51 | + |
| 52 | +<p><strong>Explanation:</strong></p> |
| 53 | + |
| 54 | +<table style="border: 1px solid black;"> |
| 55 | + <thead> |
| 56 | + <tr> |
| 57 | + <th style="border: 1px solid black;">Modification</th> |
| 58 | + <th style="border: 1px solid black;">Strategy</th> |
| 59 | + <th style="border: 1px solid black;">Profit Calculation</th> |
| 60 | + <th style="border: 1px solid black;">Profit</th> |
| 61 | + </tr> |
| 62 | + </thead> |
| 63 | + <tbody> |
| 64 | + <tr> |
| 65 | + <td style="border: 1px solid black;">Original</td> |
| 66 | + <td style="border: 1px solid black;">[-1, 0, 1]</td> |
| 67 | + <td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td> |
| 68 | + <td style="border: 1px solid black;">4</td> |
| 69 | + </tr> |
| 70 | + <tr> |
| 71 | + <td style="border: 1px solid black;">Modify [0, 1]</td> |
| 72 | + <td style="border: 1px solid black;">[0, 1, 1]</td> |
| 73 | + <td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td> |
| 74 | + <td style="border: 1px solid black;">10</td> |
| 75 | + </tr> |
| 76 | + <tr> |
| 77 | + <td style="border: 1px solid black;">Modify [1, 2]</td> |
| 78 | + <td style="border: 1px solid black;">[-1, 0, 1]</td> |
| 79 | + <td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td> |
| 80 | + <td style="border: 1px solid black;">4</td> |
| 81 | + </tr> |
| 82 | + </tbody> |
| 83 | +</table> |
| 84 | + |
| 85 | +<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>.</p> |
| 86 | +</div> |
| 87 | + |
| 88 | +<p><strong class="example">Example 2:</strong></p> |
| 89 | + |
| 90 | +<div class="example-block"> |
| 91 | +<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p> |
| 92 | + |
| 93 | +<p><strong>Output:</strong> <span class="example-io">9</span></p> |
| 94 | + |
| 95 | +<p><strong>Explanation:</strong></p> |
| 96 | + |
| 97 | +<div class="example-block"> |
| 98 | +<table style="border: 1px solid black;"> |
| 99 | + <thead> |
| 100 | + <tr> |
| 101 | + <th style="border: 1px solid black;">Modification</th> |
| 102 | + <th style="border: 1px solid black;">Strategy</th> |
| 103 | + <th style="border: 1px solid black;">Profit Calculation</th> |
| 104 | + <th style="border: 1px solid black;">Profit</th> |
| 105 | + </tr> |
| 106 | + </thead> |
| 107 | + <tbody> |
| 108 | + <tr> |
| 109 | + <td style="border: 1px solid black;">Original</td> |
| 110 | + <td style="border: 1px solid black;">[1, 1, 0]</td> |
| 111 | + <td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td> |
| 112 | + <td style="border: 1px solid black;">9</td> |
| 113 | + </tr> |
| 114 | + <tr> |
| 115 | + <td style="border: 1px solid black;">Modify [0, 1]</td> |
| 116 | + <td style="border: 1px solid black;">[0, 1, 0]</td> |
| 117 | + <td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td> |
| 118 | + <td style="border: 1px solid black;">4</td> |
| 119 | + </tr> |
| 120 | + <tr> |
| 121 | + <td style="border: 1px solid black;">Modify [1, 2]</td> |
| 122 | + <td style="border: 1px solid black;">[1, 0, 1]</td> |
| 123 | + <td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td> |
| 124 | + <td style="border: 1px solid black;">8</td> |
| 125 | + </tr> |
| 126 | + </tbody> |
| 127 | +</table> |
| 128 | + |
| 129 | +<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p> |
| 130 | +</div> |
| 131 | +</div> |
| 132 | + |
| 133 | +<p> </p> |
| 134 | +<p><strong>Constraints:</strong></p> |
| 135 | + |
| 136 | +<ul> |
| 137 | + <li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li> |
| 138 | + <li><code>1 <= prices[i] <= 10<sup>5</sup></code></li> |
| 139 | + <li><code>-1 <= strategy[i] <= 1</code></li> |
| 140 | + <li><code>2 <= k <= prices.length</code></li> |
| 141 | + <li><code>k</code> is even</li> |
| 142 | +</ul> |
| 143 | + |
| 144 | +<!-- description:end --> |
| 145 | + |
| 146 | +## Solutions |
| 147 | + |
| 148 | +<!-- solution:start --> |
| 149 | + |
| 150 | +### Solution 1 |
| 151 | + |
| 152 | +<!-- tabs:start --> |
| 153 | + |
| 154 | +#### Python3 |
| 155 | + |
| 156 | +```python |
| 157 | + |
| 158 | +``` |
| 159 | + |
| 160 | +#### Java |
| 161 | + |
| 162 | +```java |
| 163 | + |
| 164 | +``` |
| 165 | + |
| 166 | +#### C++ |
| 167 | + |
| 168 | +```cpp |
| 169 | + |
| 170 | +``` |
| 171 | + |
| 172 | +#### Go |
| 173 | + |
| 174 | +```go |
| 175 | + |
| 176 | +``` |
| 177 | + |
| 178 | +<!-- tabs:end --> |
| 179 | + |
| 180 | +<!-- solution:end --> |
| 181 | + |
| 182 | +<!-- problem:end --> |
0 commit comments