diff --git a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md index a4ed34a805161..d6d6d6e19b618 100644 --- a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md +++ b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README.md @@ -282,6 +282,44 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public long MaximumProfit(int[] prices, int k) { + int n = prices.Length; + long[,,] f = new long[n, k + 1, 3]; + + for (int j = 1; j <= k; ++j) { + f[0, j, 1] = -prices[0]; + f[0, j, 2] = prices[0]; + } + + for (int i = 1; i < n; ++i) { + for (int j = 1; j <= k; ++j) { + f[i, j, 0] = Math.Max( + f[i - 1, j, 0], + Math.Max( + f[i - 1, j, 1] + prices[i], + f[i - 1, j, 2] - prices[i] + ) + ); + f[i, j, 1] = Math.Max( + f[i - 1, j, 1], + f[i - 1, j - 1, 0] - prices[i] + ); + f[i, j, 2] = Math.Max( + f[i - 1, j, 2], + f[i - 1, j - 1, 0] + prices[i] + ); + } + } + + return f[n - 1, k, 0]; + } +} +``` + diff --git a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md index 23c668cdd0502..845bbc5d29d45 100644 --- a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md +++ b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/README_EN.md @@ -280,6 +280,44 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public long MaximumProfit(int[] prices, int k) { + int n = prices.Length; + long[,,] f = new long[n, k + 1, 3]; + + for (int j = 1; j <= k; ++j) { + f[0, j, 1] = -prices[0]; + f[0, j, 2] = prices[0]; + } + + for (int i = 1; i < n; ++i) { + for (int j = 1; j <= k; ++j) { + f[i, j, 0] = Math.Max( + f[i - 1, j, 0], + Math.Max( + f[i - 1, j, 1] + prices[i], + f[i - 1, j, 2] - prices[i] + ) + ); + f[i, j, 1] = Math.Max( + f[i - 1, j, 1], + f[i - 1, j - 1, 0] - prices[i] + ); + f[i, j, 2] = Math.Max( + f[i - 1, j, 2], + f[i - 1, j - 1, 0] + prices[i] + ); + } + } + + return f[n - 1, k, 0]; + } +} +``` + diff --git a/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/Solution.cs b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/Solution.cs new file mode 100644 index 0000000000000..b4d04212362c5 --- /dev/null +++ b/solution/3500-3599/3573.Best Time to Buy and Sell Stock V/Solution.cs @@ -0,0 +1,33 @@ +public class Solution { + public long MaximumProfit(int[] prices, int k) { + int n = prices.Length; + long[,,] f = new long[n, k + 1, 3]; + + for (int j = 1; j <= k; ++j) { + f[0, j, 1] = -prices[0]; + f[0, j, 2] = prices[0]; + } + + for (int i = 1; i < n; ++i) { + for (int j = 1; j <= k; ++j) { + f[i, j, 0] = Math.Max( + f[i - 1, j, 0], + Math.Max( + f[i - 1, j, 1] + prices[i], + f[i - 1, j, 2] - prices[i] + ) + ); + f[i, j, 1] = Math.Max( + f[i - 1, j, 1], + f[i - 1, j - 1, 0] - prices[i] + ); + f[i, j, 2] = Math.Max( + f[i - 1, j, 2], + f[i - 1, j - 1, 0] + prices[i] + ); + } + } + + return f[n - 1, k, 0]; + } +} diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md b/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md index 5b7ff7ae201d8..2aaa2214186e0 100644 --- a/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/README.md @@ -308,6 +308,47 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_gcd_score(nums: Vec, k: i32) -> i64 { + let n = nums.len(); + let mut cnt = vec![0i32; n]; + for i in 0..n { + let mut x = nums[i]; + while x % 2 == 0 { + cnt[i] += 1; + x /= 2; + } + } + + let mut ans: i64 = 0; + for l in 0..n { + let mut g: i32 = 0; + let mut mi: i32 = 1 << 30; + let mut t: i32 = 0; + for r in l..n { + g = Self::gcd(g, nums[r]); + if cnt[r] < mi { + mi = cnt[r]; + t = 1; + } else if cnt[r] == mi { + t += 1; + } + let val = if t > k { g as i64 } else { (g * 2) as i64 }; + ans = ans.max((r as i64 - l as i64 + 1) * val); + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { a } else { Self::gcd(b, a % b) } + } +} +``` + diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md b/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md index 15f81d42c5b6a..0df5503f74809 100644 --- a/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/README_EN.md @@ -304,6 +304,47 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_gcd_score(nums: Vec, k: i32) -> i64 { + let n = nums.len(); + let mut cnt = vec![0i32; n]; + for i in 0..n { + let mut x = nums[i]; + while x % 2 == 0 { + cnt[i] += 1; + x /= 2; + } + } + + let mut ans: i64 = 0; + for l in 0..n { + let mut g: i32 = 0; + let mut mi: i32 = 1 << 30; + let mut t: i32 = 0; + for r in l..n { + g = Self::gcd(g, nums[r]); + if cnt[r] < mi { + mi = cnt[r]; + t = 1; + } else if cnt[r] == mi { + t += 1; + } + let val = if t > k { g as i64 } else { (g * 2) as i64 }; + ans = ans.max((r as i64 - l as i64 + 1) * val); + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { a } else { Self::gcd(b, a % b) } + } +} +``` + diff --git a/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.rs b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.rs new file mode 100644 index 0000000000000..73142fdca2fb7 --- /dev/null +++ b/solution/3500-3599/3574.Maximize Subarray GCD Score/Solution.rs @@ -0,0 +1,36 @@ +impl Solution { + pub fn max_gcd_score(nums: Vec, k: i32) -> i64 { + let n = nums.len(); + let mut cnt = vec![0i32; n]; + for i in 0..n { + let mut x = nums[i]; + while x % 2 == 0 { + cnt[i] += 1; + x /= 2; + } + } + + let mut ans: i64 = 0; + for l in 0..n { + let mut g: i32 = 0; + let mut mi: i32 = 1 << 30; + let mut t: i32 = 0; + for r in l..n { + g = Self::gcd(g, nums[r]); + if cnt[r] < mi { + mi = cnt[r]; + t = 1; + } else if cnt[r] == mi { + t += 1; + } + let val = if t > k { g as i64 } else { (g * 2) as i64 }; + ans = ans.max((r as i64 - l as i64 + 1) * val); + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { a } else { Self::gcd(b, a % b) } + } +}