Skip to content

Commit 520728e

Browse files
authored
feat: add solutions to lc problem: No.3652 (#4728)
1 parent eb6d08f commit 520728e

File tree

7 files changed

+285
-8
lines changed

7 files changed

+285
-8
lines changed

solution/3600-3699/3652.Best Time to Buy and Sell Stock using Strategy/README.md

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,128 @@ tags:
155155

156156
<!-- solution:start -->
157157

158-
### 方法一
158+
### 方法一:前缀和 + 枚举
159+
160+
我们用一个数组 $\textit{s}$ 来表示前缀和,其中 $\textit{s}[i]$ 表示前 $i$ 天的利润和,即 $\textit{s}[i] = \sum_{j=0}^{i-1} \textit{prices}[j] \times \textit{strategy}[j]$。我们还用一个数组 $\textit{t}$ 来表示前缀和,其中 $\textit{t}[i]$ 表示前 $i$ 天的股票价格和,即 $\textit{t}[i] = \sum_{j=0}^{i-1} \textit{prices}[j]$。
161+
162+
初始时,最大利润为 $\textit{s}[n]$。我们枚举修改的子数组的右端点 $i$,则左端点为 $i-k$。修改后,子数组内前 $k/2$ 天的策略变为 $0$,后 $k/2$ 天的策略变为 $1$,因此利润变化为:
163+
164+
$$\Delta = -(\textit{s}[i] - \textit{s}[i-k]) + (\textit{t}[i] - \textit{t}[i-k/2])$$
165+
166+
因此,我们可以通过枚举所有可能的 $i$ 来更新最大利润。
167+
168+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
159169

160170
<!-- tabs:start -->
161171

162172
#### Python3
163173

164174
```python
165-
175+
class Solution:
176+
def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int:
177+
n = len(prices)
178+
s = [0] * (n + 1)
179+
t = [0] * (n + 1)
180+
for i, (a, b) in enumerate(zip(prices, strategy), 1):
181+
s[i] = s[i - 1] + a * b
182+
t[i] = t[i - 1] + a
183+
ans = s[n]
184+
for i in range(k, n + 1):
185+
ans = max(ans, s[n] - (s[i] - s[i - k]) + t[i] - t[i - k // 2])
186+
return ans
166187
```
167188

168189
#### Java
169190

170191
```java
171-
192+
class Solution {
193+
public long maxProfit(int[] prices, int[] strategy, int k) {
194+
int n = prices.length;
195+
long[] s = new long[n + 1];
196+
long[] t = new long[n + 1];
197+
for (int i = 1; i <= n; i++) {
198+
int a = prices[i - 1];
199+
int b = strategy[i - 1];
200+
s[i] = s[i - 1] + a * b;
201+
t[i] = t[i - 1] + a;
202+
}
203+
long ans = s[n];
204+
for (int i = k; i <= n; i++) {
205+
ans = Math.max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
206+
}
207+
return ans;
208+
}
209+
}
172210
```
173211

174212
#### C++
175213

176214
```cpp
177-
215+
class Solution {
216+
public:
217+
long long maxProfit(vector<int>& prices, vector<int>& strategy, int k) {
218+
int n = prices.size();
219+
vector<long long> s(n + 1), t(n + 1);
220+
for (int i = 1; i <= n; i++) {
221+
int a = prices[i - 1];
222+
int b = strategy[i - 1];
223+
s[i] = s[i - 1] + a * b;
224+
t[i] = t[i - 1] + a;
225+
}
226+
long long ans = s[n];
227+
for (int i = k; i <= n; i++) {
228+
ans = max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
229+
}
230+
return ans;
231+
}
232+
};
178233
```
179234
180235
#### Go
181236
182237
```go
238+
func maxProfit(prices []int, strategy []int, k int) int64 {
239+
n := len(prices)
240+
s := make([]int64, n+1)
241+
t := make([]int64, n+1)
242+
243+
for i := 1; i <= n; i++ {
244+
a := prices[i-1]
245+
b := strategy[i-1]
246+
s[i] = s[i-1] + int64(a*b)
247+
t[i] = t[i-1] + int64(a)
248+
}
249+
250+
ans := s[n]
251+
for i := k; i <= n; i++ {
252+
ans = max(ans, s[n]-(s[i]-s[i-k])+(t[i]-t[i-k/2]))
253+
}
254+
return ans
255+
}
256+
```
183257

258+
#### TypeScript
259+
260+
```ts
261+
function maxProfit(prices: number[], strategy: number[], k: number): number {
262+
const n = prices.length;
263+
const s: number[] = Array(n + 1).fill(0);
264+
const t: number[] = Array(n + 1).fill(0);
265+
266+
for (let i = 1; i <= n; i++) {
267+
const a = prices[i - 1];
268+
const b = strategy[i - 1];
269+
s[i] = s[i - 1] + a * b;
270+
t[i] = t[i - 1] + a;
271+
}
272+
273+
let ans = s[n];
274+
for (let i = k; i <= n; i++) {
275+
const val = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - Math.floor(k / 2)]);
276+
ans = Math.max(ans, val);
277+
}
278+
return ans;
279+
}
184280
```
185281

186282
<!-- tabs:end -->

solution/3600-3699/3652.Best Time to Buy and Sell Stock using Strategy/README_EN.md

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,32 +153,128 @@ tags:
153153

154154
<!-- solution:start -->
155155

156-
### Solution 1
156+
### Solution 1: Prefix Sum + Enumeration
157+
158+
We use an array $\textit{s}$ to represent the prefix sum, where $\textit{s}[i]$ is the total profit for the first $i$ days, i.e., $\textit{s}[i] = \sum_{j=0}^{i-1} \textit{prices}[j] \times \textit{strategy}[j]$. We also use an array $\textit{t}$ to represent the prefix sum of stock prices, where $\textit{t}[i] = \sum_{j=0}^{i-1} \textit{prices}[j]$.
159+
160+
Initially, the maximum profit is $\textit{s}[n]$. We enumerate the right endpoint $i$ of the subarray to be modified, with the left endpoint being $i-k$. After modification, the first $k/2$ days of the subarray have strategy $0$, and the last $k/2$ days have strategy $1$, so the profit change is:
161+
162+
$$\Delta = -(\textit{s}[i] - \textit{s}[i-k]) + (\textit{t}[i] - \textit{t}[i-k/2])$$
163+
164+
Therefore, we can update the maximum profit by enumerating all possible $i$.
165+
166+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array.
157167

158168
<!-- tabs:start -->
159169

160170
#### Python3
161171

162172
```python
163-
173+
class Solution:
174+
def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int:
175+
n = len(prices)
176+
s = [0] * (n + 1)
177+
t = [0] * (n + 1)
178+
for i, (a, b) in enumerate(zip(prices, strategy), 1):
179+
s[i] = s[i - 1] + a * b
180+
t[i] = t[i - 1] + a
181+
ans = s[n]
182+
for i in range(k, n + 1):
183+
ans = max(ans, s[n] - (s[i] - s[i - k]) + t[i] - t[i - k // 2])
184+
return ans
164185
```
165186

166187
#### Java
167188

168189
```java
169-
190+
class Solution {
191+
public long maxProfit(int[] prices, int[] strategy, int k) {
192+
int n = prices.length;
193+
long[] s = new long[n + 1];
194+
long[] t = new long[n + 1];
195+
for (int i = 1; i <= n; i++) {
196+
int a = prices[i - 1];
197+
int b = strategy[i - 1];
198+
s[i] = s[i - 1] + a * b;
199+
t[i] = t[i - 1] + a;
200+
}
201+
long ans = s[n];
202+
for (int i = k; i <= n; i++) {
203+
ans = Math.max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
204+
}
205+
return ans;
206+
}
207+
}
170208
```
171209

172210
#### C++
173211

174212
```cpp
175-
213+
class Solution {
214+
public:
215+
long long maxProfit(vector<int>& prices, vector<int>& strategy, int k) {
216+
int n = prices.size();
217+
vector<long long> s(n + 1), t(n + 1);
218+
for (int i = 1; i <= n; i++) {
219+
int a = prices[i - 1];
220+
int b = strategy[i - 1];
221+
s[i] = s[i - 1] + a * b;
222+
t[i] = t[i - 1] + a;
223+
}
224+
long long ans = s[n];
225+
for (int i = k; i <= n; i++) {
226+
ans = max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
227+
}
228+
return ans;
229+
}
230+
};
176231
```
177232
178233
#### Go
179234
180235
```go
236+
func maxProfit(prices []int, strategy []int, k int) int64 {
237+
n := len(prices)
238+
s := make([]int64, n+1)
239+
t := make([]int64, n+1)
240+
241+
for i := 1; i <= n; i++ {
242+
a := prices[i-1]
243+
b := strategy[i-1]
244+
s[i] = s[i-1] + int64(a*b)
245+
t[i] = t[i-1] + int64(a)
246+
}
247+
248+
ans := s[n]
249+
for i := k; i <= n; i++ {
250+
ans = max(ans, s[n]-(s[i]-s[i-k])+(t[i]-t[i-k/2]))
251+
}
252+
return ans
253+
}
254+
```
181255

256+
#### TypeScript
257+
258+
```ts
259+
function maxProfit(prices: number[], strategy: number[], k: number): number {
260+
const n = prices.length;
261+
const s: number[] = Array(n + 1).fill(0);
262+
const t: number[] = Array(n + 1).fill(0);
263+
264+
for (let i = 1; i <= n; i++) {
265+
const a = prices[i - 1];
266+
const b = strategy[i - 1];
267+
s[i] = s[i - 1] + a * b;
268+
t[i] = t[i - 1] + a;
269+
}
270+
271+
let ans = s[n];
272+
for (let i = k; i <= n; i++) {
273+
const val = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - Math.floor(k / 2)]);
274+
ans = Math.max(ans, val);
275+
}
276+
return ans;
277+
}
182278
```
183279

184280
<!-- tabs:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
long long maxProfit(vector<int>& prices, vector<int>& strategy, int k) {
4+
int n = prices.size();
5+
vector<long long> s(n + 1), t(n + 1);
6+
for (int i = 1; i <= n; i++) {
7+
int a = prices[i - 1];
8+
int b = strategy[i - 1];
9+
s[i] = s[i - 1] + a * b;
10+
t[i] = t[i - 1] + a;
11+
}
12+
long long ans = s[n];
13+
for (int i = k; i <= n; i++) {
14+
ans = max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func maxProfit(prices []int, strategy []int, k int) int64 {
2+
n := len(prices)
3+
s := make([]int64, n+1)
4+
t := make([]int64, n+1)
5+
6+
for i := 1; i <= n; i++ {
7+
a := prices[i-1]
8+
b := strategy[i-1]
9+
s[i] = s[i-1] + int64(a*b)
10+
t[i] = t[i-1] + int64(a)
11+
}
12+
13+
ans := s[n]
14+
for i := k; i <= n; i++ {
15+
ans = max(ans, s[n]-(s[i]-s[i-k])+(t[i]-t[i-k/2]))
16+
}
17+
return ans
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public long maxProfit(int[] prices, int[] strategy, int k) {
3+
int n = prices.length;
4+
long[] s = new long[n + 1];
5+
long[] t = new long[n + 1];
6+
for (int i = 1; i <= n; i++) {
7+
int a = prices[i - 1];
8+
int b = strategy[i - 1];
9+
s[i] = s[i - 1] + a * b;
10+
t[i] = t[i - 1] + a;
11+
}
12+
long ans = s[n];
13+
for (int i = k; i <= n; i++) {
14+
ans = Math.max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int:
3+
n = len(prices)
4+
s = [0] * (n + 1)
5+
t = [0] * (n + 1)
6+
for i, (a, b) in enumerate(zip(prices, strategy), 1):
7+
s[i] = s[i - 1] + a * b
8+
t[i] = t[i - 1] + a
9+
ans = s[n]
10+
for i in range(k, n + 1):
11+
ans = max(ans, s[n] - (s[i] - s[i - k]) + t[i] - t[i - k // 2])
12+
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function maxProfit(prices: number[], strategy: number[], k: number): number {
2+
const n = prices.length;
3+
const s: number[] = Array(n + 1).fill(0);
4+
const t: number[] = Array(n + 1).fill(0);
5+
6+
for (let i = 1; i <= n; i++) {
7+
const a = prices[i - 1];
8+
const b = strategy[i - 1];
9+
s[i] = s[i - 1] + a * b;
10+
t[i] = t[i - 1] + a;
11+
}
12+
13+
let ans = s[n];
14+
for (let i = k; i <= n; i++) {
15+
const val = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - Math.floor(k / 2)]);
16+
ans = Math.max(ans, val);
17+
}
18+
return ans;
19+
}

0 commit comments

Comments
 (0)