Skip to content

Commit 5ac81fb

Browse files
authored
feat: add solutions to lc problems: No.3573,3574 (#4908)
1 parent ffe44a8 commit 5ac81fb

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,44 @@ impl Solution {
282282
}
283283
```
284284

285+
#### C#
286+
287+
```cs
288+
public class Solution {
289+
public long MaximumProfit(int[] prices, int k) {
290+
int n = prices.Length;
291+
long[,,] f = new long[n, k + 1, 3];
292+
293+
for (int j = 1; j <= k; ++j) {
294+
f[0, j, 1] = -prices[0];
295+
f[0, j, 2] = prices[0];
296+
}
297+
298+
for (int i = 1; i < n; ++i) {
299+
for (int j = 1; j <= k; ++j) {
300+
f[i, j, 0] = Math.Max(
301+
f[i - 1, j, 0],
302+
Math.Max(
303+
f[i - 1, j, 1] + prices[i],
304+
f[i - 1, j, 2] - prices[i]
305+
)
306+
);
307+
f[i, j, 1] = Math.Max(
308+
f[i - 1, j, 1],
309+
f[i - 1, j - 1, 0] - prices[i]
310+
);
311+
f[i, j, 2] = Math.Max(
312+
f[i - 1, j, 2],
313+
f[i - 1, j - 1, 0] + prices[i]
314+
);
315+
}
316+
}
317+
318+
return f[n - 1, k, 0];
319+
}
320+
}
321+
```
322+
285323
<!-- tabs:end -->
286324

287325
<!-- solution:end -->

solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,44 @@ impl Solution {
280280
}
281281
```
282282

283+
#### C#
284+
285+
```cs
286+
public class Solution {
287+
public long MaximumProfit(int[] prices, int k) {
288+
int n = prices.Length;
289+
long[,,] f = new long[n, k + 1, 3];
290+
291+
for (int j = 1; j <= k; ++j) {
292+
f[0, j, 1] = -prices[0];
293+
f[0, j, 2] = prices[0];
294+
}
295+
296+
for (int i = 1; i < n; ++i) {
297+
for (int j = 1; j <= k; ++j) {
298+
f[i, j, 0] = Math.Max(
299+
f[i - 1, j, 0],
300+
Math.Max(
301+
f[i - 1, j, 1] + prices[i],
302+
f[i - 1, j, 2] - prices[i]
303+
)
304+
);
305+
f[i, j, 1] = Math.Max(
306+
f[i - 1, j, 1],
307+
f[i - 1, j - 1, 0] - prices[i]
308+
);
309+
f[i, j, 2] = Math.Max(
310+
f[i - 1, j, 2],
311+
f[i - 1, j - 1, 0] + prices[i]
312+
);
313+
}
314+
}
315+
316+
return f[n - 1, k, 0];
317+
}
318+
}
319+
```
320+
283321
<!-- tabs:end -->
284322

285323
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public long MaximumProfit(int[] prices, int k) {
3+
int n = prices.Length;
4+
long[,,] f = new long[n, k + 1, 3];
5+
6+
for (int j = 1; j <= k; ++j) {
7+
f[0, j, 1] = -prices[0];
8+
f[0, j, 2] = prices[0];
9+
}
10+
11+
for (int i = 1; i < n; ++i) {
12+
for (int j = 1; j <= k; ++j) {
13+
f[i, j, 0] = Math.Max(
14+
f[i - 1, j, 0],
15+
Math.Max(
16+
f[i - 1, j, 1] + prices[i],
17+
f[i - 1, j, 2] - prices[i]
18+
)
19+
);
20+
f[i, j, 1] = Math.Max(
21+
f[i - 1, j, 1],
22+
f[i - 1, j - 1, 0] - prices[i]
23+
);
24+
f[i, j, 2] = Math.Max(
25+
f[i - 1, j, 2],
26+
f[i - 1, j - 1, 0] + prices[i]
27+
);
28+
}
29+
}
30+
31+
return f[n - 1, k, 0];
32+
}
33+
}

solution/3500-3599/3574.Maximize Subarray GCD Score/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,47 @@ function gcd(a: number, b: number): number {
308308
}
309309
```
310310

311+
#### Rust
312+
313+
```rust
314+
impl Solution {
315+
pub fn max_gcd_score(nums: Vec<i32>, k: i32) -> i64 {
316+
let n = nums.len();
317+
let mut cnt = vec![0i32; n];
318+
for i in 0..n {
319+
let mut x = nums[i];
320+
while x % 2 == 0 {
321+
cnt[i] += 1;
322+
x /= 2;
323+
}
324+
}
325+
326+
let mut ans: i64 = 0;
327+
for l in 0..n {
328+
let mut g: i32 = 0;
329+
let mut mi: i32 = 1 << 30;
330+
let mut t: i32 = 0;
331+
for r in l..n {
332+
g = Self::gcd(g, nums[r]);
333+
if cnt[r] < mi {
334+
mi = cnt[r];
335+
t = 1;
336+
} else if cnt[r] == mi {
337+
t += 1;
338+
}
339+
let val = if t > k { g as i64 } else { (g * 2) as i64 };
340+
ans = ans.max((r as i64 - l as i64 + 1) * val);
341+
}
342+
}
343+
ans
344+
}
345+
346+
fn gcd(a: i32, b: i32) -> i32 {
347+
if b == 0 { a } else { Self::gcd(b, a % b) }
348+
}
349+
}
350+
```
351+
311352
<!-- tabs:end -->
312353

313354
<!-- solution:end -->

solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,47 @@ function gcd(a: number, b: number): number {
304304
}
305305
```
306306

307+
#### Rust
308+
309+
```rust
310+
impl Solution {
311+
pub fn max_gcd_score(nums: Vec<i32>, k: i32) -> i64 {
312+
let n = nums.len();
313+
let mut cnt = vec![0i32; n];
314+
for i in 0..n {
315+
let mut x = nums[i];
316+
while x % 2 == 0 {
317+
cnt[i] += 1;
318+
x /= 2;
319+
}
320+
}
321+
322+
let mut ans: i64 = 0;
323+
for l in 0..n {
324+
let mut g: i32 = 0;
325+
let mut mi: i32 = 1 << 30;
326+
let mut t: i32 = 0;
327+
for r in l..n {
328+
g = Self::gcd(g, nums[r]);
329+
if cnt[r] < mi {
330+
mi = cnt[r];
331+
t = 1;
332+
} else if cnt[r] == mi {
333+
t += 1;
334+
}
335+
let val = if t > k { g as i64 } else { (g * 2) as i64 };
336+
ans = ans.max((r as i64 - l as i64 + 1) * val);
337+
}
338+
}
339+
ans
340+
}
341+
342+
fn gcd(a: i32, b: i32) -> i32 {
343+
if b == 0 { a } else { Self::gcd(b, a % b) }
344+
}
345+
}
346+
```
347+
307348
<!-- tabs:end -->
308349

309350
<!-- solution:end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
impl Solution {
2+
pub fn max_gcd_score(nums: Vec<i32>, k: i32) -> i64 {
3+
let n = nums.len();
4+
let mut cnt = vec![0i32; n];
5+
for i in 0..n {
6+
let mut x = nums[i];
7+
while x % 2 == 0 {
8+
cnt[i] += 1;
9+
x /= 2;
10+
}
11+
}
12+
13+
let mut ans: i64 = 0;
14+
for l in 0..n {
15+
let mut g: i32 = 0;
16+
let mut mi: i32 = 1 << 30;
17+
let mut t: i32 = 0;
18+
for r in l..n {
19+
g = Self::gcd(g, nums[r]);
20+
if cnt[r] < mi {
21+
mi = cnt[r];
22+
t = 1;
23+
} else if cnt[r] == mi {
24+
t += 1;
25+
}
26+
let val = if t > k { g as i64 } else { (g * 2) as i64 };
27+
ans = ans.max((r as i64 - l as i64 + 1) * val);
28+
}
29+
}
30+
ans
31+
}
32+
33+
fn gcd(a: i32, b: i32) -> i32 {
34+
if b == 0 { a } else { Self::gcd(b, a % b) }
35+
}
36+
}

0 commit comments

Comments
 (0)