diff --git a/solution/0800-0899/0808.Soup Servings/README.md b/solution/0800-0899/0808.Soup Servings/README.md index d6a4fb0c35681..f294befb103e1 100644 --- a/solution/0800-0899/0808.Soup Servings/README.md +++ b/solution/0800-0899/0808.Soup Servings/README.md @@ -157,7 +157,7 @@ class Solution { public: double soupServings(int n) { double f[200][200] = {0.0}; - function dfs = [&](int i, int j) -> double { + auto dfs = [&](this auto&& dfs, int i, int j) -> double { if (i <= 0 && j <= 0) return 0.5; if (i <= 0) return 1; if (j <= 0) return 0; @@ -205,7 +205,7 @@ func soupServings(n int) float64 { ```ts function soupServings(n: number): number { - const f = new Array(200).fill(0).map(() => new Array(200).fill(-1)); + const f = Array.from({ length: 200 }, () => Array(200).fill(-1)); const dfs = (i: number, j: number): number => { if (i <= 0 && j <= 0) { return 0.5; @@ -227,6 +227,77 @@ function soupServings(n: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn soup_servings(n: i32) -> f64 { + if n > 4800 { + return 1.0; + } + Self::dfs((n + 24) / 25, (n + 24) / 25) + } + + fn dfs(i: i32, j: i32) -> f64 { + static mut F: [[f64; 200]; 200] = [[0.0; 200]; 200]; + + unsafe { + if i <= 0 && j <= 0 { + return 0.5; + } + if i <= 0 { + return 1.0; + } + if j <= 0 { + return 0.0; + } + if F[i as usize][j as usize] > 0.0 { + return F[i as usize][j as usize]; + } + + let ans = 0.25 * (Self::dfs(i - 4, j) + Self::dfs(i - 3, j - 1) + Self::dfs(i - 2, j - 2) + Self::dfs(i - 1, j - 3)); + F[i as usize][j as usize] = ans; + ans + } + } +} +``` + +#### C# + +```cs +public class Solution { + private double[,] f = new double[200, 200]; + + public double SoupServings(int n) { + if (n > 4800) { + return 1.0; + } + + return Dfs((n + 24) / 25, (n + 24) / 25); + } + + private double Dfs(int i, int j) { + if (i <= 0 && j <= 0) { + return 0.5; + } + if (i <= 0) { + return 1.0; + } + if (j <= 0) { + return 0.0; + } + if (f[i, j] > 0) { + return f[i, j]; + } + + double ans = 0.25 * (Dfs(i - 4, j) + Dfs(i - 3, j - 1) + Dfs(i - 2, j - 2) + Dfs(i - 1, j - 3)); + f[i, j] = ans; + return ans; + } +} +``` + diff --git a/solution/0800-0899/0808.Soup Servings/README_EN.md b/solution/0800-0899/0808.Soup Servings/README_EN.md index 46661ed7c7bef..07bca39d5b0c1 100644 --- a/solution/0800-0899/0808.Soup Servings/README_EN.md +++ b/solution/0800-0899/0808.Soup Servings/README_EN.md @@ -155,7 +155,7 @@ class Solution { public: double soupServings(int n) { double f[200][200] = {0.0}; - function dfs = [&](int i, int j) -> double { + auto dfs = [&](this auto&& dfs, int i, int j) -> double { if (i <= 0 && j <= 0) return 0.5; if (i <= 0) return 1; if (j <= 0) return 0; @@ -203,7 +203,7 @@ func soupServings(n int) float64 { ```ts function soupServings(n: number): number { - const f = new Array(200).fill(0).map(() => new Array(200).fill(-1)); + const f = Array.from({ length: 200 }, () => Array(200).fill(-1)); const dfs = (i: number, j: number): number => { if (i <= 0 && j <= 0) { return 0.5; @@ -225,6 +225,77 @@ function soupServings(n: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn soup_servings(n: i32) -> f64 { + if n > 4800 { + return 1.0; + } + Self::dfs((n + 24) / 25, (n + 24) / 25) + } + + fn dfs(i: i32, j: i32) -> f64 { + static mut F: [[f64; 200]; 200] = [[0.0; 200]; 200]; + + unsafe { + if i <= 0 && j <= 0 { + return 0.5; + } + if i <= 0 { + return 1.0; + } + if j <= 0 { + return 0.0; + } + if F[i as usize][j as usize] > 0.0 { + return F[i as usize][j as usize]; + } + + let ans = 0.25 * (Self::dfs(i - 4, j) + Self::dfs(i - 3, j - 1) + Self::dfs(i - 2, j - 2) + Self::dfs(i - 1, j - 3)); + F[i as usize][j as usize] = ans; + ans + } + } +} +``` + +#### C# + +```cs +public class Solution { + private double[,] f = new double[200, 200]; + + public double SoupServings(int n) { + if (n > 4800) { + return 1.0; + } + + return Dfs((n + 24) / 25, (n + 24) / 25); + } + + private double Dfs(int i, int j) { + if (i <= 0 && j <= 0) { + return 0.5; + } + if (i <= 0) { + return 1.0; + } + if (j <= 0) { + return 0.0; + } + if (f[i, j] > 0) { + return f[i, j]; + } + + double ans = 0.25 * (Dfs(i - 4, j) + Dfs(i - 3, j - 1) + Dfs(i - 2, j - 2) + Dfs(i - 1, j - 3)); + f[i, j] = ans; + return ans; + } +} +``` + diff --git a/solution/0800-0899/0808.Soup Servings/Solution.cpp b/solution/0800-0899/0808.Soup Servings/Solution.cpp index c9553e7e3eb65..7dc8b14365c4a 100644 --- a/solution/0800-0899/0808.Soup Servings/Solution.cpp +++ b/solution/0800-0899/0808.Soup Servings/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: double soupServings(int n) { double f[200][200] = {0.0}; - function dfs = [&](int i, int j) -> double { + auto dfs = [&](this auto&& dfs, int i, int j) -> double { if (i <= 0 && j <= 0) return 0.5; if (i <= 0) return 1; if (j <= 0) return 0; diff --git a/solution/0800-0899/0808.Soup Servings/Solution.cs b/solution/0800-0899/0808.Soup Servings/Solution.cs new file mode 100644 index 0000000000000..0cbe75c41c284 --- /dev/null +++ b/solution/0800-0899/0808.Soup Servings/Solution.cs @@ -0,0 +1,30 @@ +public class Solution { + private double[,] f = new double[200, 200]; + + public double SoupServings(int n) { + if (n > 4800) { + return 1.0; + } + + return Dfs((n + 24) / 25, (n + 24) / 25); + } + + private double Dfs(int i, int j) { + if (i <= 0 && j <= 0) { + return 0.5; + } + if (i <= 0) { + return 1.0; + } + if (j <= 0) { + return 0.0; + } + if (f[i, j] > 0) { + return f[i, j]; + } + + double ans = 0.25 * (Dfs(i - 4, j) + Dfs(i - 3, j - 1) + Dfs(i - 2, j - 2) + Dfs(i - 1, j - 3)); + f[i, j] = ans; + return ans; + } +} diff --git a/solution/0800-0899/0808.Soup Servings/Solution.rs b/solution/0800-0899/0808.Soup Servings/Solution.rs new file mode 100644 index 0000000000000..177c2f271cd1a --- /dev/null +++ b/solution/0800-0899/0808.Soup Servings/Solution.rs @@ -0,0 +1,35 @@ +impl Solution { + pub fn soup_servings(n: i32) -> f64 { + if n > 4800 { + return 1.0; + } + Self::dfs((n + 24) / 25, (n + 24) / 25) + } + + fn dfs(i: i32, j: i32) -> f64 { + static mut F: [[f64; 200]; 200] = [[0.0; 200]; 200]; + + unsafe { + if i <= 0 && j <= 0 { + return 0.5; + } + if i <= 0 { + return 1.0; + } + if j <= 0 { + return 0.0; + } + if F[i as usize][j as usize] > 0.0 { + return F[i as usize][j as usize]; + } + + let ans = 0.25 + * (Self::dfs(i - 4, j) + + Self::dfs(i - 3, j - 1) + + Self::dfs(i - 2, j - 2) + + Self::dfs(i - 1, j - 3)); + F[i as usize][j as usize] = ans; + ans + } + } +} diff --git a/solution/0800-0899/0808.Soup Servings/Solution.ts b/solution/0800-0899/0808.Soup Servings/Solution.ts index 4afce5c07f60f..d6e1b08085e6a 100644 --- a/solution/0800-0899/0808.Soup Servings/Solution.ts +++ b/solution/0800-0899/0808.Soup Servings/Solution.ts @@ -1,5 +1,5 @@ function soupServings(n: number): number { - const f = new Array(200).fill(0).map(() => new Array(200).fill(-1)); + const f = Array.from({ length: 200 }, () => Array(200).fill(-1)); const dfs = (i: number, j: number): number => { if (i <= 0 && j <= 0) { return 0.5;