Skip to content

Commit aafcb19

Browse files
authored
feat: add solutions to lc problem: No.3652 (#4909)
1 parent 5ac81fb commit aafcb19

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,66 @@ function maxProfit(prices: number[], strategy: number[], k: number): number {
279279
}
280280
```
281281

282+
#### Rust
283+
284+
```rust
285+
impl Solution {
286+
pub fn max_profit(prices: Vec<i32>, strategy: Vec<i32>, k: i32) -> i64 {
287+
let n: usize = prices.len();
288+
let k: usize = k as usize;
289+
290+
let mut s: Vec<i64> = vec![0; n + 1];
291+
let mut t: Vec<i64> = vec![0; n + 1];
292+
293+
for i in 1..=n {
294+
let a: i64 = prices[i - 1] as i64;
295+
let b: i64 = strategy[i - 1] as i64;
296+
s[i] = s[i - 1] + a * b;
297+
t[i] = t[i - 1] + a;
298+
}
299+
300+
let mut ans: i64 = s[n];
301+
for i in k..=n {
302+
let cur = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]);
303+
if cur > ans {
304+
ans = cur;
305+
}
306+
}
307+
308+
ans
309+
}
310+
}
311+
```
312+
313+
#### C#
314+
315+
```cs
316+
public class Solution {
317+
public long MaxProfit(int[] prices, int[] strategy, int k) {
318+
int n = prices.Length;
319+
long[] s = new long[n + 1];
320+
long[] t = new long[n + 1];
321+
322+
for (int i = 1; i <= n; i++) {
323+
long a = prices[i - 1];
324+
long b = strategy[i - 1];
325+
s[i] = s[i - 1] + a * b;
326+
t[i] = t[i - 1] + a;
327+
}
328+
329+
long ans = s[n];
330+
for (int i = k; i <= n; i++) {
331+
long cur = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]);
332+
if (cur > ans) {
333+
ans = cur;
334+
}
335+
}
336+
337+
return ans;
338+
}
339+
}
340+
```
341+
282342
<!-- tabs:end -->
283343

284344
<!-- solution:end -->

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,66 @@ function maxProfit(prices: number[], strategy: number[], k: number): number {
277277
}
278278
```
279279

280+
#### Rust
281+
282+
```rust
283+
impl Solution {
284+
pub fn max_profit(prices: Vec<i32>, strategy: Vec<i32>, k: i32) -> i64 {
285+
let n: usize = prices.len();
286+
let k: usize = k as usize;
287+
288+
let mut s: Vec<i64> = vec![0; n + 1];
289+
let mut t: Vec<i64> = vec![0; n + 1];
290+
291+
for i in 1..=n {
292+
let a: i64 = prices[i - 1] as i64;
293+
let b: i64 = strategy[i - 1] as i64;
294+
s[i] = s[i - 1] + a * b;
295+
t[i] = t[i - 1] + a;
296+
}
297+
298+
let mut ans: i64 = s[n];
299+
for i in k..=n {
300+
let cur = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]);
301+
if cur > ans {
302+
ans = cur;
303+
}
304+
}
305+
306+
ans
307+
}
308+
}
309+
```
310+
311+
#### C#
312+
313+
```cs
314+
public class Solution {
315+
public long MaxProfit(int[] prices, int[] strategy, int k) {
316+
int n = prices.Length;
317+
long[] s = new long[n + 1];
318+
long[] t = new long[n + 1];
319+
320+
for (int i = 1; i <= n; i++) {
321+
long a = prices[i - 1];
322+
long b = strategy[i - 1];
323+
s[i] = s[i - 1] + a * b;
324+
t[i] = t[i - 1] + a;
325+
}
326+
327+
long ans = s[n];
328+
for (int i = k; i <= n; i++) {
329+
long cur = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]);
330+
if (cur > ans) {
331+
ans = cur;
332+
}
333+
}
334+
335+
return ans;
336+
}
337+
}
338+
```
339+
280340
<!-- tabs:end -->
281341

282342
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public 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+
7+
for (int i = 1; i <= n; i++) {
8+
long a = prices[i - 1];
9+
long b = strategy[i - 1];
10+
s[i] = s[i - 1] + a * b;
11+
t[i] = t[i - 1] + a;
12+
}
13+
14+
long ans = s[n];
15+
for (int i = k; i <= n; i++) {
16+
long cur = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]);
17+
if (cur > ans) {
18+
ans = cur;
19+
}
20+
}
21+
22+
return ans;
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn max_profit(prices: Vec<i32>, strategy: Vec<i32>, k: i32) -> i64 {
3+
let n: usize = prices.len();
4+
let k: usize = k as usize;
5+
6+
let mut s: Vec<i64> = vec![0; n + 1];
7+
let mut t: Vec<i64> = vec![0; n + 1];
8+
9+
for i in 1..=n {
10+
let a: i64 = prices[i - 1] as i64;
11+
let b: i64 = strategy[i - 1] as i64;
12+
s[i] = s[i - 1] + a * b;
13+
t[i] = t[i - 1] + a;
14+
}
15+
16+
let mut ans: i64 = s[n];
17+
for i in k..=n {
18+
let cur = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]);
19+
if cur > ans {
20+
ans = cur;
21+
}
22+
}
23+
24+
ans
25+
}
26+
}

0 commit comments

Comments
 (0)