@@ -157,7 +157,7 @@ class Solution {
157
157
public:
158
158
double soupServings(int n) {
159
159
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 {
161
161
if (i <= 0 && j <= 0) return 0.5;
162
162
if (i <= 0) return 1;
163
163
if (j <= 0) return 0;
@@ -205,7 +205,7 @@ func soupServings(n int) float64 {
205
205
206
206
``` ts
207
207
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 ));
209
209
const dfs = (i : number , j : number ): number => {
210
210
if (i <= 0 && j <= 0 ) {
211
211
return 0.5 ;
@@ -227,6 +227,77 @@ function soupServings(n: number): number {
227
227
}
228
228
```
229
229
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
+
230
301
<!-- tabs: end -->
231
302
232
303
<!-- solution: end -->
0 commit comments