You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2189.Number of Ways to Build House of Cards/README_EN.md
+16-2
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,21 @@ The third house of cards uses 2 cards.
71
71
72
72
<!-- solution:start -->
73
73
74
-
### Solution 1
74
+
### Solution 1: Memoization Search
75
+
76
+
We notice that the number of cards in each layer is $3 \times k + 2$, and the number of cards in each layer is different. Therefore, the problem can be transformed into: how many ways can the integer $n$ be expressed as the sum of numbers of the form $3 \times k + 2$. This is a classic knapsack problem that can be solved using memoization search.
77
+
78
+
We design a function $\text{dfs}(n, k)$, which represents the number of ways to build different houses of cards when the remaining number of cards is $n$ and the current layer is $k$. The answer is $\text{dfs}(n, 0)$.
79
+
80
+
The execution logic of the function $\text{dfs}(n, k)$ is as follows:
81
+
82
+
- If $3 \times k + 2 \gt n$, then the current layer cannot place any cards, return $0$;
83
+
- If $3 \times k + 2 = n$, then the current layer can place cards, and after placing them, the entire house of cards is completed, return $1$;
84
+
- Otherwise, we can choose not to place cards or to place cards. If we choose not to place cards, the remaining number of cards does not change, and the number of layers increases by $1$, i.e., $\text{dfs}(n, k + 1)$. If we choose to place cards, the remaining number of cards decreases by $3 \times k + 2$, and the number of layers increases by $1$, i.e., $\text{dfs}(n - (3 \times k + 2), k + 1)$. The sum of these two cases is the answer.
85
+
86
+
During the process, we can use memoization to avoid repeated calculations.
87
+
88
+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of cards.
75
89
76
90
<!-- tabs:start -->
77
91
@@ -127,7 +141,7 @@ public:
127
141
int houseOfCards(int n) {
128
142
int f[n + 1][n / 3 + 1];
129
143
memset(f, -1, sizeof(f));
130
-
function<int(int, int)> dfs = [&](int n, int k) -> int {
144
+
auto dfs = [&](this auto&& dfs, int n, int k) -> int {
Copy file name to clipboardexpand all lines: solution/2200-2299/2214.Minimum Health to Beat Game/README_EN.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -82,9 +82,9 @@ Note that you did not use your armor ability.
82
82
83
83
### Solution 1: Greedy
84
84
85
-
We can greedily choose to use the armor skill in the round with the maximum damage. Suppose the maximum damage is $mx$, then we can avoid $min(mx, armor)$ damage, so the minimum life value we need is $sum(damage) - min(mx, armor) + 1$.
85
+
We can greedily choose to use the armor skill in the round with the highest damage. Suppose the maximum damage is $\textit{mx}$, then we can avoid $\min(\textit{mx}, \textit{armor})$ damage. Therefore, the minimum health required is $\sum(\textit{damage}) - \min(\textit{mx}, \textit{armor}) + 1$.
86
86
87
-
The time complexity is $O(n)$, where $n$ is the length of the `damage`array. The space complexity is $O(1)$.
87
+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{damage}$. The space complexity is $O(1)$.
0 commit comments