Skip to content

Commit 0d80d37

Browse files
authored
Merge branch 'main' into dev
2 parents dd52f13 + 535b4fd commit 0d80d37

File tree

13 files changed

+1071
-1
lines changed

13 files changed

+1071
-1
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3652.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20using%20Strategy/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3652. 按策略买卖股票的最佳时机](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy)
10+
11+
[English Version](/solution/3600-3699/3652.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20using%20Strategy/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你两个整数数组 <code>prices</code> 和 <code>strategy</code>,其中:</p>
18+
19+
<ul>
20+
<li><code>prices[i]</code> 表示第 <code>i</code> 天某股票的价格。</li>
21+
<li><code>strategy[i]</code> 表示第 <code>i</code> 天的交易策略,其中:
22+
<ul>
23+
<li><code>-1</code> 表示买入一单位股票。</li>
24+
<li><code>0</code> 表示持有股票。</li>
25+
<li><code>1</code> 表示卖出一单位股票。</li>
26+
</ul>
27+
</li>
28+
</ul>
29+
30+
<p>同时给你一个&nbsp;<strong>偶数&nbsp;</strong>整数 <code>k</code>,你可以对 <code>strategy</code> 进行&nbsp;<strong>最多一次&nbsp;</strong>修改。一次修改包括:</p>
31+
32+
<ul>
33+
<li>选择 <code>strategy</code> 中恰好 <code>k</code> 个&nbsp;<strong>连续&nbsp;</strong>元素。</li>
34+
<li>将前 <code>k / 2</code> 个元素设为 <code>0</code>(持有)。</li>
35+
<li>将后 <code>k / 2</code> 个元素设为 <code>1</code>(卖出)。</li>
36+
</ul>
37+
38+
<p><strong>利润&nbsp;</strong>定义为所有天数中 <code>strategy[i] * prices[i]</code> 的&nbsp;<strong>总和&nbsp;</strong>。</p>
39+
40+
<p>返回你可以获得的&nbsp;<strong>最大&nbsp;</strong>可能利润。</p>
41+
42+
<p><strong>注意:</strong> 没有预算或股票持有数量的限制,因此所有买入和卖出操作均可行,无需考虑过去的操作。</p>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong class="example">示例 1:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>输入:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
50+
51+
<p><strong>输出:</strong> <span class="example-io">10</span></p>
52+
53+
<p><strong>解释:</strong></p>
54+
55+
<table style="border: 1px solid black;">
56+
<thead>
57+
<tr>
58+
<th style="border: 1px solid black;">修改</th>
59+
<th style="border: 1px solid black;">策略</th>
60+
<th style="border: 1px solid black;">利润计算</th>
61+
<th style="border: 1px solid black;">利润</th>
62+
</tr>
63+
</thead>
64+
<tbody>
65+
<tr>
66+
<td style="border: 1px solid black;">原始</td>
67+
<td style="border: 1px solid black;">[-1, 0, 1]</td>
68+
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
69+
<td style="border: 1px solid black;">4</td>
70+
</tr>
71+
<tr>
72+
<td style="border: 1px solid black;">修改 [0, 1]</td>
73+
<td style="border: 1px solid black;">[0, 1, 1]</td>
74+
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
75+
<td style="border: 1px solid black;">10</td>
76+
</tr>
77+
<tr>
78+
<td style="border: 1px solid black;">修改 [1, 2]</td>
79+
<td style="border: 1px solid black;">[-1, 0, 1]</td>
80+
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
81+
<td style="border: 1px solid black;">4</td>
82+
</tr>
83+
</tbody>
84+
</table>
85+
86+
<p>因此,最大可能利润是 10,通过修改子数组 <code>[0, 1]</code> 实现。</p>
87+
</div>
88+
89+
<p><strong class="example">示例 2:</strong></p>
90+
91+
<div class="example-block">
92+
<p><strong>输入:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
93+
94+
<p><strong>输出:</strong> <span class="example-io">9</span></p>
95+
96+
<p><strong>解释:</strong></p>
97+
98+
<div class="example-block">
99+
<table style="border: 1px solid black;">
100+
<thead>
101+
<tr>
102+
<th style="border: 1px solid black;">修改</th>
103+
<th style="border: 1px solid black;">策略</th>
104+
<th style="border: 1px solid black;">利润计算</th>
105+
<th style="border: 1px solid black;">利润</th>
106+
</tr>
107+
</thead>
108+
<tbody>
109+
<tr>
110+
<td style="border: 1px solid black;">原始</td>
111+
<td style="border: 1px solid black;">[1, 1, 0]</td>
112+
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
113+
<td style="border: 1px solid black;">9</td>
114+
</tr>
115+
<tr>
116+
<td style="border: 1px solid black;">修改 [0, 1]</td>
117+
<td style="border: 1px solid black;">[0, 1, 0]</td>
118+
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
119+
<td style="border: 1px solid black;">4</td>
120+
</tr>
121+
<tr>
122+
<td style="border: 1px solid black;">修改 [1, 2]</td>
123+
<td style="border: 1px solid black;">[1, 0, 1]</td>
124+
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
125+
<td style="border: 1px solid black;">8</td>
126+
</tr>
127+
</tbody>
128+
</table>
129+
130+
<p>因此,最大可能利润是 9,无需任何修改即可达成。</p>
131+
</div>
132+
</div>
133+
134+
<p>&nbsp;</p>
135+
136+
<p><strong>提示:</strong></p>
137+
138+
<ul>
139+
<li><code>2 &lt;= prices.length == strategy.length &lt;= 10<sup>5</sup></code></li>
140+
<li><code>1 &lt;= prices[i] &lt;= 10<sup>5</sup></code></li>
141+
<li><code>-1 &lt;= strategy[i] &lt;= 1</code></li>
142+
<li><code>2 &lt;= k &lt;= prices.length</code></li>
143+
<li><code>k</code> 是偶数</li>
144+
</ul>
145+
146+
<!-- description:end -->
147+
148+
## 解法
149+
150+
<!-- solution:start -->
151+
152+
### 方法一
153+
154+
<!-- tabs:start -->
155+
156+
#### Python3
157+
158+
```python
159+
160+
```
161+
162+
#### Java
163+
164+
```java
165+
166+
```
167+
168+
#### C++
169+
170+
```cpp
171+
172+
```
173+
174+
#### Go
175+
176+
```go
177+
178+
```
179+
180+
<!-- tabs:end -->
181+
182+
<!-- solution:end -->
183+
184+
<!-- problem:end -->
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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>&nbsp;</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 &times; 4) + (0 &times; 2) + (1 &times; 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 &times; 4) + (1 &times; 2) + (1 &times; 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 &times; 4) + (0 &times; 2) + (1 &times; 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 &times; 5) + (1 &times; 4) + (0 &times; 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 &times; 5) + (1 &times; 4) + (0 &times; 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 &times; 5) + (0 &times; 4) + (1 &times; 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>&nbsp;</p>
134+
<p><strong>Constraints:</strong></p>
135+
136+
<ul>
137+
<li><code>2 &lt;= prices.length == strategy.length &lt;= 10<sup>5</sup></code></li>
138+
<li><code>1 &lt;= prices[i] &lt;= 10<sup>5</sup></code></li>
139+
<li><code>-1 &lt;= strategy[i] &lt;= 1</code></li>
140+
<li><code>2 &lt;= k &lt;= 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

Comments
 (0)