Skip to content

Commit 0c57289

Browse files
authored
feat: add solutions to lc problem: No.0808 (#4627)
No.0808.Soup Servings
1 parent 9ef585e commit 0c57289

File tree

6 files changed

+213
-6
lines changed

6 files changed

+213
-6
lines changed

solution/0800-0899/0808.Soup Servings/README.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class Solution {
157157
public:
158158
double soupServings(int n) {
159159
double f[200][200] = {0.0};
160-
function<double(int, int)> dfs = [&](int i, int j) -> double {
160+
auto dfs = [&](this auto&& dfs, int i, int j) -> double {
161161
if (i <= 0 && j <= 0) return 0.5;
162162
if (i <= 0) return 1;
163163
if (j <= 0) return 0;
@@ -205,7 +205,7 @@ func soupServings(n int) float64 {
205205

206206
```ts
207207
function soupServings(n: number): number {
208-
const f = new Array(200).fill(0).map(() => new Array(200).fill(-1));
208+
const f = Array.from({ length: 200 }, () => Array(200).fill(-1));
209209
const dfs = (i: number, j: number): number => {
210210
if (i <= 0 && j <= 0) {
211211
return 0.5;
@@ -227,6 +227,77 @@ function soupServings(n: number): number {
227227
}
228228
```
229229

230+
#### Rust
231+
232+
```rust
233+
impl Solution {
234+
pub fn soup_servings(n: i32) -> f64 {
235+
if n > 4800 {
236+
return 1.0;
237+
}
238+
Self::dfs((n + 24) / 25, (n + 24) / 25)
239+
}
240+
241+
fn dfs(i: i32, j: i32) -> f64 {
242+
static mut F: [[f64; 200]; 200] = [[0.0; 200]; 200];
243+
244+
unsafe {
245+
if i <= 0 && j <= 0 {
246+
return 0.5;
247+
}
248+
if i <= 0 {
249+
return 1.0;
250+
}
251+
if j <= 0 {
252+
return 0.0;
253+
}
254+
if F[i as usize][j as usize] > 0.0 {
255+
return F[i as usize][j as usize];
256+
}
257+
258+
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));
259+
F[i as usize][j as usize] = ans;
260+
ans
261+
}
262+
}
263+
}
264+
```
265+
266+
#### C#
267+
268+
```cs
269+
public class Solution {
270+
private double[,] f = new double[200, 200];
271+
272+
public double SoupServings(int n) {
273+
if (n > 4800) {
274+
return 1.0;
275+
}
276+
277+
return Dfs((n + 24) / 25, (n + 24) / 25);
278+
}
279+
280+
private double Dfs(int i, int j) {
281+
if (i <= 0 && j <= 0) {
282+
return 0.5;
283+
}
284+
if (i <= 0) {
285+
return 1.0;
286+
}
287+
if (j <= 0) {
288+
return 0.0;
289+
}
290+
if (f[i, j] > 0) {
291+
return f[i, j];
292+
}
293+
294+
double ans = 0.25 * (Dfs(i - 4, j) + Dfs(i - 3, j - 1) + Dfs(i - 2, j - 2) + Dfs(i - 1, j - 3));
295+
f[i, j] = ans;
296+
return ans;
297+
}
298+
}
299+
```
300+
230301
<!-- tabs:end -->
231302

232303
<!-- solution:end -->

solution/0800-0899/0808.Soup Servings/README_EN.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Solution {
155155
public:
156156
double soupServings(int n) {
157157
double f[200][200] = {0.0};
158-
function<double(int, int)> dfs = [&](int i, int j) -> double {
158+
auto dfs = [&](this auto&& dfs, int i, int j) -> double {
159159
if (i <= 0 && j <= 0) return 0.5;
160160
if (i <= 0) return 1;
161161
if (j <= 0) return 0;
@@ -203,7 +203,7 @@ func soupServings(n int) float64 {
203203

204204
```ts
205205
function soupServings(n: number): number {
206-
const f = new Array(200).fill(0).map(() => new Array(200).fill(-1));
206+
const f = Array.from({ length: 200 }, () => Array(200).fill(-1));
207207
const dfs = (i: number, j: number): number => {
208208
if (i <= 0 && j <= 0) {
209209
return 0.5;
@@ -225,6 +225,77 @@ function soupServings(n: number): number {
225225
}
226226
```
227227

228+
#### Rust
229+
230+
```rust
231+
impl Solution {
232+
pub fn soup_servings(n: i32) -> f64 {
233+
if n > 4800 {
234+
return 1.0;
235+
}
236+
Self::dfs((n + 24) / 25, (n + 24) / 25)
237+
}
238+
239+
fn dfs(i: i32, j: i32) -> f64 {
240+
static mut F: [[f64; 200]; 200] = [[0.0; 200]; 200];
241+
242+
unsafe {
243+
if i <= 0 && j <= 0 {
244+
return 0.5;
245+
}
246+
if i <= 0 {
247+
return 1.0;
248+
}
249+
if j <= 0 {
250+
return 0.0;
251+
}
252+
if F[i as usize][j as usize] > 0.0 {
253+
return F[i as usize][j as usize];
254+
}
255+
256+
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));
257+
F[i as usize][j as usize] = ans;
258+
ans
259+
}
260+
}
261+
}
262+
```
263+
264+
#### C#
265+
266+
```cs
267+
public class Solution {
268+
private double[,] f = new double[200, 200];
269+
270+
public double SoupServings(int n) {
271+
if (n > 4800) {
272+
return 1.0;
273+
}
274+
275+
return Dfs((n + 24) / 25, (n + 24) / 25);
276+
}
277+
278+
private double Dfs(int i, int j) {
279+
if (i <= 0 && j <= 0) {
280+
return 0.5;
281+
}
282+
if (i <= 0) {
283+
return 1.0;
284+
}
285+
if (j <= 0) {
286+
return 0.0;
287+
}
288+
if (f[i, j] > 0) {
289+
return f[i, j];
290+
}
291+
292+
double ans = 0.25 * (Dfs(i - 4, j) + Dfs(i - 3, j - 1) + Dfs(i - 2, j - 2) + Dfs(i - 1, j - 3));
293+
f[i, j] = ans;
294+
return ans;
295+
}
296+
}
297+
```
298+
228299
<!-- tabs:end -->
229300

230301
<!-- solution:end -->

solution/0800-0899/0808.Soup Servings/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
double soupServings(int n) {
44
double f[200][200] = {0.0};
5-
function<double(int, int)> dfs = [&](int i, int j) -> double {
5+
auto dfs = [&](this auto&& dfs, int i, int j) -> double {
66
if (i <= 0 && j <= 0) return 0.5;
77
if (i <= 0) return 1;
88
if (j <= 0) return 0;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
private double[,] f = new double[200, 200];
3+
4+
public double SoupServings(int n) {
5+
if (n > 4800) {
6+
return 1.0;
7+
}
8+
9+
return Dfs((n + 24) / 25, (n + 24) / 25);
10+
}
11+
12+
private double Dfs(int i, int j) {
13+
if (i <= 0 && j <= 0) {
14+
return 0.5;
15+
}
16+
if (i <= 0) {
17+
return 1.0;
18+
}
19+
if (j <= 0) {
20+
return 0.0;
21+
}
22+
if (f[i, j] > 0) {
23+
return f[i, j];
24+
}
25+
26+
double ans = 0.25 * (Dfs(i - 4, j) + Dfs(i - 3, j - 1) + Dfs(i - 2, j - 2) + Dfs(i - 1, j - 3));
27+
f[i, j] = ans;
28+
return ans;
29+
}
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn soup_servings(n: i32) -> f64 {
3+
if n > 4800 {
4+
return 1.0;
5+
}
6+
Self::dfs((n + 24) / 25, (n + 24) / 25)
7+
}
8+
9+
fn dfs(i: i32, j: i32) -> f64 {
10+
static mut F: [[f64; 200]; 200] = [[0.0; 200]; 200];
11+
12+
unsafe {
13+
if i <= 0 && j <= 0 {
14+
return 0.5;
15+
}
16+
if i <= 0 {
17+
return 1.0;
18+
}
19+
if j <= 0 {
20+
return 0.0;
21+
}
22+
if F[i as usize][j as usize] > 0.0 {
23+
return F[i as usize][j as usize];
24+
}
25+
26+
let ans = 0.25
27+
* (Self::dfs(i - 4, j)
28+
+ Self::dfs(i - 3, j - 1)
29+
+ Self::dfs(i - 2, j - 2)
30+
+ Self::dfs(i - 1, j - 3));
31+
F[i as usize][j as usize] = ans;
32+
ans
33+
}
34+
}
35+
}

solution/0800-0899/0808.Soup Servings/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function soupServings(n: number): number {
2-
const f = new Array(200).fill(0).map(() => new Array(200).fill(-1));
2+
const f = Array.from({ length: 200 }, () => Array(200).fill(-1));
33
const dfs = (i: number, j: number): number => {
44
if (i <= 0 && j <= 0) {
55
return 0.5;

0 commit comments

Comments
 (0)