1
[[nodiscard]] double step(int const A, int const B,
2
std::unordered_map<int, std::unordered_map<int, double>> &Memo) {
3
if (A <= 0) return B <= 0 ? 0.5 : 1.0;
4
if (B <= 0) return 0.0;
5

6
auto const AIt{Memo.find(A)};
7

8
if (Memo.cend() != AIt) {
9
auto const BIt{AIt->second.find(B)};
10
if (AIt->second.cend() != BIt) return BIt->second;
11
}
12

13
double M{step(A - 100, B, Memo)};
14
M += step(A - 75, B - 25, Memo);
15
M += step(A - 50, B - 50, Memo);
16
M += step(A - 25, B - 75, Memo);
17
return Memo[A][B] = 0.25 * M;
18
}
19

20
class Solution {
21
public:
22
double soupServings(int n) {
23
if (n > 4750) return 1;
24

25
std::unordered_map<int, std::unordered_map<int, double>> Memo{};
26
return step(n, n, Memo);
27
}
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0