|
47 | 47 |
|
48 | 48 | <!-- 这里可写通用的实现逻辑 -->
|
49 | 49 |
|
| 50 | +**方法一:二进制枚举** |
| 51 | + |
| 52 | +我们注意到,料理的数量 $n$ 不超过 $8$,因此,我们可以使用二进制枚举的方法枚举所有的料理方案。 |
| 53 | + |
| 54 | +每种料理都有两种选择:制作或者不制作。因此,我们可以使用一个长度为 $8$ 的二进制数来表示一种方案,其中第 $i$ 位为 $1$ 表示制作第 $i$ 道料理,为 $0$ 表示不制作第 $i$ 道料理。 |
| 55 | + |
| 56 | +我们在 $[0, 2^n)$ 的范围内枚举所有料理方案,对于每种方案,我们计算其美味度和饱腹感,如果饱腹感不小于 $limit$,并且食材数量足够制作这些料理,同时其美味度大于当前答案,我们就更新答案。 |
| 57 | + |
| 58 | +枚举结束后,我们就可以得到最大的美味度。 |
| 59 | + |
| 60 | +时间复杂度 $(2^n \times n)$,其中 $n$ 是料理的数量。我们需要枚举所有的料理方案,对于每种方案,我们需要 $O(n)$ 的时间计算其美味度和饱腹感,因此总时间复杂度为 $O(2^n \times n)$。空间复杂度 $O(1)$。 |
| 61 | + |
50 | 62 | <!-- tabs:start -->
|
51 | 63 |
|
52 | 64 | ### **Python3**
|
53 | 65 |
|
54 | 66 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 67 |
|
56 | 68 | ```python
|
57 |
| - |
| 69 | +class Solution: |
| 70 | + def perfectMenu( |
| 71 | + self, |
| 72 | + materials: List[int], |
| 73 | + cookbooks: List[List[int]], |
| 74 | + attribute: List[List[int]], |
| 75 | + limit: int, |
| 76 | + ) -> int: |
| 77 | + n = len(cookbooks) |
| 78 | + ans = -1 |
| 79 | + for mask in range(1 << n): |
| 80 | + a = b = 0 |
| 81 | + cnt = [0] * 5 |
| 82 | + for i in range(n): |
| 83 | + if mask >> i & 1: |
| 84 | + x, y = attribute[i] |
| 85 | + a += x |
| 86 | + b += y |
| 87 | + for j, v in enumerate(cookbooks[i]): |
| 88 | + cnt[j] += v |
| 89 | + if b >= limit and ans < a and all(c <= d for c, d in zip(cnt, materials)): |
| 90 | + ans = a |
| 91 | + return ans |
58 | 92 | ```
|
59 | 93 |
|
60 | 94 | ### **Java**
|
61 | 95 |
|
62 | 96 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 97 |
|
64 | 98 | ```java
|
| 99 | +class Solution { |
| 100 | + public int perfectMenu(int[] materials, int[][] cookbooks, int[][] attribute, int limit) { |
| 101 | + int n = cookbooks.length; |
| 102 | + int ans = -1; |
| 103 | + for (int mask = 0; mask < 1 << n; ++mask) { |
| 104 | + int a = 0, b = 0; |
| 105 | + int[] cnt = new int[5]; |
| 106 | + for (int i = 0; i < n; ++i) { |
| 107 | + if ((mask >> i & 1) == 1) { |
| 108 | + int x = attribute[i][0]; |
| 109 | + int y = attribute[i][1]; |
| 110 | + a += x; |
| 111 | + b += y; |
| 112 | + for (int j = 0; j < cookbooks[i].length; ++j) { |
| 113 | + cnt[j] += cookbooks[i][j]; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + boolean ok = true; |
| 118 | + for (int i = 0; i < 5 && ok; ++i) { |
| 119 | + ok = cnt[i] <= materials[i]; |
| 120 | + } |
| 121 | + if (b >= limit && ans < a && ok) { |
| 122 | + ans = a; |
| 123 | + } |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### **C++** |
| 131 | + |
| 132 | +```cpp |
| 133 | +class Solution { |
| 134 | +public: |
| 135 | + int perfectMenu(vector<int>& materials, vector<vector<int>>& cookbooks, vector<vector<int>>& attribute, int limit) { |
| 136 | + int n = cookbooks.size(); |
| 137 | + int ans = -1; |
| 138 | + for (int mask = 0; mask < 1 << n; ++mask) { |
| 139 | + int a = 0, b = 0; |
| 140 | + vector<int> cnt(5); |
| 141 | + for (int i = 0; i < n; ++i) { |
| 142 | + if (mask >> i & 1) { |
| 143 | + int x = attribute[i][0]; |
| 144 | + int y = attribute[i][1]; |
| 145 | + a += x; |
| 146 | + b += y; |
| 147 | + for (int j = 0; j < cookbooks[i].size(); ++j) { |
| 148 | + cnt[j] += cookbooks[i][j]; |
| 149 | + } |
| 150 | + } |
| 151 | + bool ok = true; |
| 152 | + for (int i = 0; i < 5 && ok; ++i) { |
| 153 | + ok = cnt[i] <= materials[i]; |
| 154 | + } |
| 155 | + if (b >= limit && ans < a && ok) { |
| 156 | + ans = a; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + return ans; |
| 161 | + } |
| 162 | +}; |
| 163 | +``` |
| 164 | +
|
| 165 | +### **Go** |
| 166 | +
|
| 167 | +```go |
| 168 | +func perfectMenu(materials []int, cookbooks [][]int, attribute [][]int, limit int) int { |
| 169 | + n := len(cookbooks) |
| 170 | + ans := -1 |
| 171 | + for mask := 0; mask < 1<<n; mask++ { |
| 172 | + a, b := 0, 0 |
| 173 | + cnt := make([]int, 5) |
| 174 | + for i := 0; i < n; i++ { |
| 175 | + if mask>>i&1 == 1 { |
| 176 | + x, y := attribute[i][0], attribute[i][1] |
| 177 | + a += x |
| 178 | + b += y |
| 179 | + for j, v := range cookbooks[i] { |
| 180 | + cnt[j] += v |
| 181 | + } |
| 182 | + } |
| 183 | + ok := true |
| 184 | + for i := 0; i < 5 && ok; i++ { |
| 185 | + ok = cnt[i] <= materials[i] |
| 186 | + } |
| 187 | + if ok && b >= limit && ans < a { |
| 188 | + ans = a |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + return ans |
| 193 | +} |
| 194 | +``` |
65 | 195 |
|
| 196 | +### **TypeScript** |
| 197 | + |
| 198 | +```ts |
| 199 | +function perfectMenu( |
| 200 | + materials: number[], |
| 201 | + cookbooks: number[][], |
| 202 | + attribute: number[][], |
| 203 | + limit: number, |
| 204 | +): number { |
| 205 | + const n = cookbooks.length; |
| 206 | + let ans = -1; |
| 207 | + for (let mask = 0; mask < 1 << n; ++mask) { |
| 208 | + let [a, b] = [0, 0]; |
| 209 | + const cnt: number[] = Array(5).fill(0); |
| 210 | + for (let i = 0; i < n; ++i) { |
| 211 | + if (((mask >> i) & 1) === 1) { |
| 212 | + const [x, y] = attribute[i]; |
| 213 | + a += x; |
| 214 | + b += y; |
| 215 | + for (let j = 0; j < cookbooks[i].length; ++j) { |
| 216 | + cnt[j] += cookbooks[i][j]; |
| 217 | + } |
| 218 | + } |
| 219 | + let ok = true; |
| 220 | + for (let i = 0; i < 5 && ok; ++i) { |
| 221 | + ok = cnt[i] <= materials[i]; |
| 222 | + } |
| 223 | + if (b >= limit && ans < a && ok) { |
| 224 | + ans = a; |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + return ans; |
| 229 | +} |
66 | 230 | ```
|
67 | 231 |
|
68 | 232 | ### **...**
|
|
0 commit comments