Skip to content

Commit d4eefa7

Browse files
committed
update: 添加问题“3652.按策略买卖股票的最佳时机”的代码和题解 (#1266)
3652: AC.cpp (#1265) + word(en) + 1problem1day 1problem1day from branch 3562(#1262) Signed-off-by: LetMeFly666 <[email protected]>
1 parent 9d320bf commit d4eefa7

File tree

7 files changed

+309
-1
lines changed

7 files changed

+309
-1
lines changed

.commitmsg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
3652: AC.cpp (#1265) + word(en) + 1problem1day
2+
3+
1problem1day from branch 3562(#1262)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Auto add 1problem1day issue to 1problem1day Project
2+
3+
on:
4+
issues:
5+
types: [opened, edited]
6+
7+
permissions:
8+
contents: read
9+
issues: read
10+
repository-projects: write
11+
12+
jobs:
13+
add-to-project:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Check title and add to project
17+
env:
18+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
PROJECT_ID: ${{ secrets.ONE_PROBLEM_ONE_DAY_PROJECT_ID }}
20+
STATUS_FIELD_ID: ${{ secrets.ONE_PROBLEM_ONE_DAY_STATUS_FIELD_ID }}
21+
DATE_FIELD_ID: ${{ secrets.ONE_PROBLEM_ONE_DAY_DATE_FIELD_ID }}
22+
run: |
23+
title="${{ github.event.issue.title }}"
24+
issue_id="${{ github.event.issue.node_id }}"
25+
created_at="${{ github.event.issue.created_at }}"
26+
date_cn=$(date -d "$created_at +8 hours" +%F)
27+
28+
if [[ ! "$title" =~ ^\[newSolution\]Who\ can\ add\ 1\ more\ problem\ of\ LeetCode\ [0-9]+.*$ ]]; then
29+
echo "Title not match, skip"
30+
exit 0
31+
fi
32+
33+
echo "Adding issue to project..."
34+
35+
item_id=$(gh api graphql -f query='
36+
mutation($project:ID!, $content:ID!) {
37+
addProjectV2ItemById(projectId:$project, contentId:$content) {
38+
item { id }
39+
}
40+
}' -f project="$PROJECT_ID" -f content="$issue_id" --jq '.data.addProjectV2ItemById.item.id')
41+
42+
echo "Set Status = TODO"
43+
gh api graphql -f query='
44+
mutation($item:ID!, $field:ID!, $value:String!) {
45+
updateProjectV2ItemFieldValue(
46+
itemId:$item
47+
fieldId:$field
48+
value:{ singleSelectOptionId:$value }
49+
) { projectV2Item { id } }
50+
}' \
51+
-f item="$item_id" \
52+
-f field="$STATUS_FIELD_ID" \
53+
-f value="TODO"
54+
55+
echo "Set Date"
56+
gh api graphql -f query='
57+
mutation($item:ID!, $field:ID!, $value:Date!) {
58+
updateProjectV2ItemFieldValue(
59+
itemId:$item
60+
fieldId:$field
61+
value:{ date:$value }
62+
) { projectV2Item { id } }
63+
}' \
64+
-f item="$item_id" \
65+
-f field="$DATE_FIELD_ID" \
66+
-f value="${date_cn}"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-18 13:17:04
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-18 18:42:50
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
public:
14+
ll maxProfit(vector<int>& prices, vector<int>& strategy, int k) {
15+
ll ans = 0;
16+
int n = prices.size();
17+
for (int i = 0; i < n; i++) {
18+
ans += strategy[i] * prices[i];
19+
}
20+
21+
ll now = ans;
22+
for (int i = 0; i < k / 2; i++) {
23+
now += (0 - strategy[i]) * prices[i];
24+
}
25+
for (int i = k / 2; i < k; i++) {
26+
now += (1 - strategy[i]) * prices[i];
27+
}
28+
ans = max(ans, now);
29+
30+
for (int i = 1; i + k <= n; i++) {
31+
// i-1: 0->original
32+
// i+k/2-1: 1->0
33+
// i+k-1: original->1
34+
now += (strategy[i - 1] - 0) * prices[i - 1] + (0 - 1) * prices[i + k/2 - 1] + (1 - strategy[i + k - 1]) * prices[i + k - 1];
35+
ans = max(ans, now);
36+
}
37+
return ans;
38+
}
39+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@
10611061
|3577.统计计算机解锁顺序排列数|中等|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/10/LeetCode%203577.%E7%BB%9F%E8%AE%A1%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%A3%E9%94%81%E9%A1%BA%E5%BA%8F%E6%8E%92%E5%88%97%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155791805" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/solutions/3854206/letmefly-3577tong-ji-ji-suan-ji-jie-suo-5b6b8/" target="_blank">LeetCode题解</a>|
10621062
|3583.统计特殊三元组|中等|<a href="https://leetcode.cn/problems/count-special-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/09/LeetCode%203583.%E7%BB%9F%E8%AE%A1%E7%89%B9%E6%AE%8A%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155754955" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-special-triplets/solutions/3853381/letmefly-3583tong-ji-te-shu-san-yuan-zu-cdnx3/" target="_blank">LeetCode题解</a>|
10631063
|3606.优惠券校验器|简单|<a href="https://leetcode.cn/problems/coupon-code-validator/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/13/LeetCode%203606.%E4%BC%98%E6%83%A0%E5%88%B8%E6%A0%A1%E9%AA%8C%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155893803" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/coupon-code-validator/solutions/3856383/letmefly-3606you-hui-quan-xiao-yan-qi-fe-vqir/" target="_blank">LeetCode题解</a>|
1064+
|3652.按策略买卖股票的最佳时机|中等|<a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/18/LeetCode%203652.%E6%8C%89%E7%AD%96%E7%95%A5%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156061117" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/solutions/3860245/letmefly-3652an-ce-lue-mai-mai-gu-piao-d-cnx2/" target="_blank">LeetCode题解</a>|
10641065
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
10651066
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
10661067
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 3573.买卖股票的最佳时机V.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private:
116116

117117
ll dfs(int i, int j, int status) {
118118
// 0~i天 最多交易j次
119-
// status: 0无1买2空头
119+
// status: 0 无仓(空仓) 1 持有多头 2 持有空头
120120

121121
int key = getKey(i, j, status);
122122
if (cache.count(key)) {
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
---
2+
title: 3652.按策略买卖股票的最佳时机:滑动窗口
3+
date: 2025-12-18 18:45:15
4+
tags: [题解, LeetCode, 中等, 数组, 前缀和, 滑动窗口]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3652.按策略买卖股票的最佳时机:滑动窗口
9+
10+
力扣题目链接:[https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/)
11+
12+
<p>给你两个整数数组 <code>prices</code> 和 <code>strategy</code>,其中:</p>
13+
14+
<ul>
15+
<li><code>prices[i]</code> 表示第 <code>i</code> 天某股票的价格。</li>
16+
<li><code>strategy[i]</code> 表示第 <code>i</code> 天的交易策略,其中:
17+
<ul>
18+
<li><code>-1</code> 表示买入一单位股票。</li>
19+
<li><code>0</code> 表示持有股票。</li>
20+
<li><code>1</code> 表示卖出一单位股票。</li>
21+
</ul>
22+
</li>
23+
</ul>
24+
25+
<p>同时给你一个&nbsp;<strong>偶数&nbsp;</strong>整数 <code>k</code>,你可以对 <code>strategy</code> 进行&nbsp;<strong>最多一次&nbsp;</strong>修改。一次修改包括:</p>
26+
27+
<ul>
28+
<li>选择 <code>strategy</code> 中恰好 <code>k</code> 个&nbsp;<strong>连续&nbsp;</strong>元素。</li>
29+
<li>将前 <code>k / 2</code> 个元素设为 <code>0</code>(持有)。</li>
30+
<li>将后 <code>k / 2</code> 个元素设为 <code>1</code>(卖出)。</li>
31+
</ul>
32+
33+
<p><strong>利润&nbsp;</strong>定义为所有天数中 <code>strategy[i] * prices[i]</code> 的&nbsp;<strong>总和&nbsp;</strong>。</p>
34+
35+
<p>返回你可以获得的&nbsp;<strong>最大&nbsp;</strong>可能利润。</p>
36+
37+
<p><strong>注意:</strong> 没有预算或股票持有数量的限制,因此所有买入和卖出操作均可行,无需考虑过去的操作。</p>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong class="example">示例 1:</strong></p>
42+
43+
<div class="example-block">
44+
<p><strong>输入:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
45+
46+
<p><strong>输出:</strong> <span class="example-io">10</span></p>
47+
48+
<p><strong>解释:</strong></p>
49+
50+
<table style="border: 1px solid black;">
51+
<thead>
52+
<tr>
53+
<th style="border: 1px solid black;">修改</th>
54+
<th style="border: 1px solid black;">策略</th>
55+
<th style="border: 1px solid black;">利润计算</th>
56+
<th style="border: 1px solid black;">利润</th>
57+
</tr>
58+
</thead>
59+
<tbody>
60+
<tr>
61+
<td style="border: 1px solid black;">原始</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+
<tr>
67+
<td style="border: 1px solid black;">修改 [0, 1]</td>
68+
<td style="border: 1px solid black;">[0, 1, 1]</td>
69+
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
70+
<td style="border: 1px solid black;">10</td>
71+
</tr>
72+
<tr>
73+
<td style="border: 1px solid black;">修改 [1, 2]</td>
74+
<td style="border: 1px solid black;">[-1, 0, 1]</td>
75+
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
76+
<td style="border: 1px solid black;">4</td>
77+
</tr>
78+
</tbody>
79+
</table>
80+
81+
<p>因此,最大可能利润是 10,通过修改子数组 <code>[0, 1]</code> 实现。</p>
82+
</div>
83+
84+
<p><strong class="example">示例 2:</strong></p>
85+
86+
<div class="example-block">
87+
<p><strong>输入:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
88+
89+
<p><strong>输出:</strong> <span class="example-io">9</span></p>
90+
91+
<p><strong>解释:</strong></p>
92+
93+
<div class="example-block">
94+
<table style="border: 1px solid black;">
95+
<thead>
96+
<tr>
97+
<th style="border: 1px solid black;">修改</th>
98+
<th style="border: 1px solid black;">策略</th>
99+
<th style="border: 1px solid black;">利润计算</th>
100+
<th style="border: 1px solid black;">利润</th>
101+
</tr>
102+
</thead>
103+
<tbody>
104+
<tr>
105+
<td style="border: 1px solid black;">原始</td>
106+
<td style="border: 1px solid black;">[1, 1, 0]</td>
107+
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
108+
<td style="border: 1px solid black;">9</td>
109+
</tr>
110+
<tr>
111+
<td style="border: 1px solid black;">修改 [0, 1]</td>
112+
<td style="border: 1px solid black;">[0, 1, 0]</td>
113+
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
114+
<td style="border: 1px solid black;">4</td>
115+
</tr>
116+
<tr>
117+
<td style="border: 1px solid black;">修改 [1, 2]</td>
118+
<td style="border: 1px solid black;">[1, 0, 1]</td>
119+
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
120+
<td style="border: 1px solid black;">8</td>
121+
</tr>
122+
</tbody>
123+
</table>
124+
125+
<p>因此,最大可能利润是 9,无需任何修改即可达成。</p>
126+
</div>
127+
</div>
128+
129+
<p>&nbsp;</p>
130+
131+
<p><strong>提示:</strong></p>
132+
133+
<ul>
134+
<li><code>2 &lt;= prices.length == strategy.length &lt;= 10<sup>5</sup></code></li>
135+
<li><code>1 &lt;= prices[i] &lt;= 10<sup>5</sup></code></li>
136+
<li><code>-1 &lt;= strategy[i] &lt;= 1</code></li>
137+
<li><code>2 &lt;= k &lt;= prices.length</code></li>
138+
<li><code>k</code> 是偶数</li>
139+
</ul>
140+
141+
142+
143+
## 解题方法:滑动窗口
144+
145+
既然修改范围是定长的,并且最多修改1次,那么就从前往后将每一种修改可能都试试呗。
146+
147+
初始先计算原数组不修改时收益,再从前往后依次尝试修改区间,取收益最大的一个作为答案。
148+
149+
如何从一个区间快速计算出下一个区间呢?变化的有3个:(变化前的)区间起点、区间中点、区间终点,把这三个位置的值更新一下就好了。
150+
151+
+ 时间复杂度$O(len(prices))$
152+
+ 空间复杂度$O(1)$
153+
154+
### AC代码
155+
156+
#### C++
157+
158+
```cpp
159+
/*
160+
* @LastEditTime: 2025-12-18 18:42:50
161+
*/
162+
typedef long long ll;
163+
class Solution {
164+
public:
165+
ll maxProfit(vector<int>& prices, vector<int>& strategy, int k) {
166+
ll ans = 0;
167+
int n = prices.size();
168+
for (int i = 0; i < n; i++) {
169+
ans += strategy[i] * prices[i];
170+
}
171+
172+
ll now = ans;
173+
for (int i = 0; i < k / 2; i++) {
174+
now += (0 - strategy[i]) * prices[i];
175+
}
176+
for (int i = k / 2; i < k; i++) {
177+
now += (1 - strategy[i]) * prices[i];
178+
}
179+
ans = max(ans, now);
180+
181+
for (int i = 1; i + k <= n; i++) {
182+
// i-1: 0->original
183+
// i+k/2-1: 1->0
184+
// i+k-1: original->1
185+
now += (strategy[i - 1] - 0) * prices[i - 1] + (0 - 1) * prices[i + k/2 - 1] + (1 - strategy[i + k - 1]) * prices[i + k - 1];
186+
ans = max(ans, now);
187+
}
188+
return ans;
189+
}
190+
};
191+
```
192+
193+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/156061117)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/18/LeetCode%203652.%E6%8C%89%E7%AD%96%E7%95%A5%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA/)~
194+
>
195+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,10 @@ categories: [自用]
16761676
|||
16771677
|bossy|adj. 好指挥人的,专横的<br/>n. 牛|
16781678
|scarlet|adj. 猩红的,鲜红的<details><summary>例句</summary>Her lips are <font color="#28bea0">scarlet</font>.<br/>她嘴唇鲜红。</details>|
1679+
|||
1680+
|procession|n. 行列,列队行进,游行,(一个接着一个而来的)一队人|
1681+
|buffalo|v. 威吓,欺骗,迷惑<br/>n. 水牛,(北美)野牛|
1682+
|hoe|n. 锄头<br/>v. 锄地|
16791683

16801684
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
16811685
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)