|
62 | 62 |
|
63 | 63 | <!-- solution:start -->
|
64 | 64 |
|
65 |
| -### 方法一 |
| 65 | +### 方法一:DFS |
| 66 | + |
| 67 | +我们不妨假设整数分别为 $a_1, a_2, \cdots, a_n$,它们的深度分别为 $d_1, d_2, \cdots, d_n$,最大深度为 $\text{maxDepth}$,那么答案就是: |
| 68 | + |
| 69 | +$$ |
| 70 | +a_1 \times \text{maxDepth} - a_1 \times d_1 + a_1 + a_2 \times \text{maxDepth} - a_2 \times d_2 + a_2 + \cdots + a_n \times \text{maxDepth} - a_n \times d_n + a_n |
| 71 | +$$ |
| 72 | + |
| 73 | +即: |
| 74 | + |
| 75 | +$$ |
| 76 | +(\text{maxDepth} + 1) \times (a_1 + a_2 + \cdots + a_n) - (a_1 \times d_1 + a_2 \times d_2 + \cdots + a_n \times d_n) |
| 77 | +$$ |
| 78 | + |
| 79 | +如果我们记所有整数的和为 $s$,所有整数乘以深度的和为 $ws$,那么答案就是: |
| 80 | + |
| 81 | +$$ |
| 82 | +(\text{maxDepth} + 1) \times s - ws |
| 83 | +$$ |
| 84 | + |
| 85 | +因此,我们设计一个函数 $dfs(x, d)$,表示从 $x$ 开始,深度为 $d$ 开始搜索,函数 $dfs(x, d)$ 的执行过程如下: |
| 86 | + |
| 87 | +- 我们先更新 $\text{maxDepth} = \max(\text{maxDepth}, d)$; |
| 88 | +- 如果 $x$ 是一个整数,那么我们更新 $s = s + x$, $ws = ws + x \times d$; |
| 89 | +- 否则,我们递归地遍历 $x$ 的每一个元素 $y$,调用 $dfs(y, d + 1)$。 |
| 90 | + |
| 91 | +我们遍历整个列表,对于每一个元素 $x$,我们调用 $dfs(x, 1)$,最终返回 $(\text{maxDepth} + 1) \times s - ws$ 即可。 |
| 92 | + |
| 93 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为整数的个数。 |
66 | 94 |
|
67 | 95 | <!-- tabs:start -->
|
68 | 96 |
|
@@ -113,25 +141,20 @@ tags:
|
113 | 141 | # """
|
114 | 142 | class Solution:
|
115 | 143 | def depthSumInverse(self, nestedList: List[NestedInteger]) -> int:
|
116 |
| - def max_depth(nestedList): |
117 |
| - depth = 1 |
118 |
| - for item in nestedList: |
119 |
| - if item.isInteger(): |
120 |
| - continue |
121 |
| - depth = max(depth, max_depth(item.getList()) + 1) |
122 |
| - return depth |
123 |
| - |
124 |
| - def dfs(nestedList, max_depth): |
125 |
| - depth_sum = 0 |
126 |
| - for item in nestedList: |
127 |
| - if item.isInteger(): |
128 |
| - depth_sum += item.getInteger() * max_depth |
129 |
| - else: |
130 |
| - depth_sum += dfs(item.getList(), max_depth - 1) |
131 |
| - return depth_sum |
132 |
| - |
133 |
| - depth = max_depth(nestedList) |
134 |
| - return dfs(nestedList, depth) |
| 144 | + def dfs(x, d): |
| 145 | + nonlocal maxDepth, s, ws |
| 146 | + maxDepth = max(maxDepth, d) |
| 147 | + if x.isInteger(): |
| 148 | + s += x.getInteger() |
| 149 | + ws += x.getInteger() * d |
| 150 | + else: |
| 151 | + for y in x.getList(): |
| 152 | + dfs(y, d + 1) |
| 153 | + |
| 154 | + maxDepth = s = ws = 0 |
| 155 | + for x in nestedList: |
| 156 | + dfs(x, 1) |
| 157 | + return (maxDepth + 1) * s - ws |
135 | 158 | ```
|
136 | 159 |
|
137 | 160 | #### Java
|
@@ -166,33 +189,194 @@ class Solution:
|
166 | 189 | * }
|
167 | 190 | */
|
168 | 191 | class Solution {
|
| 192 | + private int maxDepth; |
| 193 | + private int ws; |
| 194 | + private int s; |
| 195 | + |
169 | 196 | public int depthSumInverse(List<NestedInteger> nestedList) {
|
170 |
| - int depth = maxDepth(nestedList); |
171 |
| - return dfs(nestedList, depth); |
| 197 | + for (NestedInteger x : nestedList) { |
| 198 | + dfs(x, 1); |
| 199 | + } |
| 200 | + return (maxDepth + 1) * s - ws; |
172 | 201 | }
|
173 | 202 |
|
174 |
| - private int maxDepth(List<NestedInteger> nestedList) { |
175 |
| - int depth = 1; |
176 |
| - for (NestedInteger item : nestedList) { |
177 |
| - if (item.isInteger()) { |
178 |
| - continue; |
| 203 | + private void dfs(NestedInteger x, int d) { |
| 204 | + maxDepth = Math.max(maxDepth, d); |
| 205 | + if (x.isInteger()) { |
| 206 | + ws += x.getInteger() * d; |
| 207 | + s += x.getInteger(); |
| 208 | + } else { |
| 209 | + for (NestedInteger y : x.getList()) { |
| 210 | + dfs(y, d + 1); |
179 | 211 | }
|
180 |
| - depth = Math.max(depth, 1 + maxDepth(item.getList())); |
181 | 212 | }
|
182 |
| - return depth; |
183 | 213 | }
|
| 214 | +} |
| 215 | +``` |
184 | 216 |
|
185 |
| - private int dfs(List<NestedInteger> nestedList, int depth) { |
186 |
| - int depthSum = 0; |
187 |
| - for (NestedInteger item : nestedList) { |
188 |
| - if (item.isInteger()) { |
189 |
| - depthSum += item.getInteger() * depth; |
| 217 | +#### C++ |
| 218 | + |
| 219 | +```cpp |
| 220 | +/** |
| 221 | + * // This is the interface that allows for creating nested lists. |
| 222 | + * // You should not implement it, or speculate about its implementation |
| 223 | + * class NestedInteger { |
| 224 | + * public: |
| 225 | + * // Constructor initializes an empty nested list. |
| 226 | + * NestedInteger(); |
| 227 | + * |
| 228 | + * // Constructor initializes a single integer. |
| 229 | + * NestedInteger(int value); |
| 230 | + * |
| 231 | + * // Return true if this NestedInteger holds a single integer, rather than a nested list. |
| 232 | + * bool isInteger() const; |
| 233 | + * |
| 234 | + * // Return the single integer that this NestedInteger holds, if it holds a single integer |
| 235 | + * // The result is undefined if this NestedInteger holds a nested list |
| 236 | + * int getInteger() const; |
| 237 | + * |
| 238 | + * // Set this NestedInteger to hold a single integer. |
| 239 | + * void setInteger(int value); |
| 240 | + * |
| 241 | + * // Set this NestedInteger to hold a nested list and adds a nested integer to it. |
| 242 | + * void add(const NestedInteger &ni); |
| 243 | + * |
| 244 | + * // Return the nested list that this NestedInteger holds, if it holds a nested list |
| 245 | + * // The result is undefined if this NestedInteger holds a single integer |
| 246 | + * const vector<NestedInteger> &getList() const; |
| 247 | + * }; |
| 248 | + */ |
| 249 | +class Solution { |
| 250 | +public: |
| 251 | + int depthSumInverse(vector<NestedInteger>& nestedList) { |
| 252 | + int maxDepth = 0, ws = 0, s = 0; |
| 253 | + function<void(NestedInteger&, int)> dfs = [&](NestedInteger& x, int d) { |
| 254 | + maxDepth = max(maxDepth, d); |
| 255 | + if (x.isInteger()) { |
| 256 | + ws += x.getInteger() * d; |
| 257 | + s += x.getInteger(); |
190 | 258 | } else {
|
191 |
| - depthSum += dfs(item.getList(), depth - 1); |
| 259 | + for (auto& y : x.getList()) { |
| 260 | + dfs(y, d + 1); |
| 261 | + } |
| 262 | + } |
| 263 | + }; |
| 264 | + for (auto& x : nestedList) { |
| 265 | + dfs(x, 1); |
| 266 | + } |
| 267 | + return (maxDepth + 1) * s - ws; |
| 268 | + } |
| 269 | +}; |
| 270 | +``` |
| 271 | +
|
| 272 | +#### Go |
| 273 | +
|
| 274 | +```go |
| 275 | +/** |
| 276 | + * // This is the interface that allows for creating nested lists. |
| 277 | + * // You should not implement it, or speculate about its implementation |
| 278 | + * type NestedInteger struct { |
| 279 | + * } |
| 280 | + * |
| 281 | + * // Return true if this NestedInteger holds a single integer, rather than a nested list. |
| 282 | + * func (n NestedInteger) IsInteger() bool {} |
| 283 | + * |
| 284 | + * // Return the single integer that this NestedInteger holds, if it holds a single integer |
| 285 | + * // The result is undefined if this NestedInteger holds a nested list |
| 286 | + * // So before calling this method, you should have a check |
| 287 | + * func (n NestedInteger) GetInteger() int {} |
| 288 | + * |
| 289 | + * // Set this NestedInteger to hold a single integer. |
| 290 | + * func (n *NestedInteger) SetInteger(value int) {} |
| 291 | + * |
| 292 | + * // Set this NestedInteger to hold a nested list and adds a nested integer to it. |
| 293 | + * func (n *NestedInteger) Add(elem NestedInteger) {} |
| 294 | + * |
| 295 | + * // Return the nested list that this NestedInteger holds, if it holds a nested list |
| 296 | + * // The list length is zero if this NestedInteger holds a single integer |
| 297 | + * // You can access NestedInteger's List element directly if you want to modify it |
| 298 | + * func (n NestedInteger) GetList() []*NestedInteger {} |
| 299 | + */ |
| 300 | +func depthSumInverse(nestedList []*NestedInteger) int { |
| 301 | + var maxDepth, ws, s int |
| 302 | + var dfs func(*NestedInteger, int) |
| 303 | + dfs = func(x *NestedInteger, d int) { |
| 304 | + maxDepth = max(maxDepth, d) |
| 305 | + if x.IsInteger() { |
| 306 | + ws += x.GetInteger() * d |
| 307 | + s += x.GetInteger() |
| 308 | + } else { |
| 309 | + for _, y := range x.GetList() { |
| 310 | + dfs(y, d+1) |
| 311 | + } |
| 312 | + } |
| 313 | + } |
| 314 | + for _, x := range nestedList { |
| 315 | + dfs(x, 1) |
| 316 | + } |
| 317 | + return (maxDepth+1)*s - ws |
| 318 | +} |
| 319 | +``` |
| 320 | + |
| 321 | +#### TypeScript |
| 322 | + |
| 323 | +```ts |
| 324 | +/** |
| 325 | + * // This is the interface that allows for creating nested lists. |
| 326 | + * // You should not implement it, or speculate about its implementation |
| 327 | + * class NestedInteger { |
| 328 | + * If value is provided, then it holds a single integer |
| 329 | + * Otherwise it holds an empty nested list |
| 330 | + * constructor(value?: number) { |
| 331 | + * ... |
| 332 | + * }; |
| 333 | + * |
| 334 | + * Return true if this NestedInteger holds a single integer, rather than a nested list. |
| 335 | + * isInteger(): boolean { |
| 336 | + * ... |
| 337 | + * }; |
| 338 | + * |
| 339 | + * Return the single integer that this NestedInteger holds, if it holds a single integer |
| 340 | + * Return null if this NestedInteger holds a nested list |
| 341 | + * getInteger(): number | null { |
| 342 | + * ... |
| 343 | + * }; |
| 344 | + * |
| 345 | + * Set this NestedInteger to hold a single integer equal to value. |
| 346 | + * setInteger(value: number) { |
| 347 | + * ... |
| 348 | + * }; |
| 349 | + * |
| 350 | + * Set this NestedInteger to hold a nested list and adds a nested integer elem to it. |
| 351 | + * add(elem: NestedInteger) { |
| 352 | + * ... |
| 353 | + * }; |
| 354 | + * |
| 355 | + * Return the nested list that this NestedInteger holds, |
| 356 | + * or an empty list if this NestedInteger holds a single integer |
| 357 | + * getList(): NestedInteger[] { |
| 358 | + * ... |
| 359 | + * }; |
| 360 | + * }; |
| 361 | + */ |
| 362 | + |
| 363 | +function depthSumInverse(nestedList: NestedInteger[]): number { |
| 364 | + let [maxDepth, ws, s] = [0, 0, 0]; |
| 365 | + const dfs = (x: NestedInteger, d: number) => { |
| 366 | + maxDepth = Math.max(maxDepth, d); |
| 367 | + if (x.isInteger()) { |
| 368 | + ws += x.getInteger() * d; |
| 369 | + s += x.getInteger(); |
| 370 | + } else { |
| 371 | + for (const y of x.getList()) { |
| 372 | + dfs(y, d + 1); |
192 | 373 | }
|
193 | 374 | }
|
194 |
| - return depthSum; |
| 375 | + }; |
| 376 | + for (const x of nestedList) { |
| 377 | + dfs(x, 1); |
195 | 378 | }
|
| 379 | + return (maxDepth + 1) * s - ws; |
196 | 380 | }
|
197 | 381 | ```
|
198 | 382 |
|
@@ -242,29 +426,22 @@ class Solution {
|
242 | 426 | * @return {number}
|
243 | 427 | */
|
244 | 428 | var depthSumInverse = function (nestedList) {
|
245 |
| - const maxDepth = nestedList => { |
246 |
| - let depth = 1; |
247 |
| - for (const item of nestedList) { |
248 |
| - if (item.isInteger()) { |
249 |
| - continue; |
| 429 | + let [maxDepth, ws, s] = [0, 0, 0]; |
| 430 | + const dfs = (x, d) => { |
| 431 | + maxDepth = Math.max(maxDepth, d); |
| 432 | + if (x.isInteger()) { |
| 433 | + ws += x.getInteger() * d; |
| 434 | + s += x.getInteger(); |
| 435 | + } else { |
| 436 | + for (const y of x.getList()) { |
| 437 | + dfs(y, d + 1); |
250 | 438 | }
|
251 |
| - depth = Math.max(depth, 1 + maxDepth(item.getList())); |
252 | 439 | }
|
253 |
| - return depth; |
254 | 440 | };
|
255 |
| - const dfs = (nestedList, depth) => { |
256 |
| - let depthSum = 0; |
257 |
| - for (const item of nestedList) { |
258 |
| - if (item.isInteger()) { |
259 |
| - depthSum += item.getInteger() * depth; |
260 |
| - } else { |
261 |
| - depthSum += dfs(item.getList(), depth - 1); |
262 |
| - } |
263 |
| - } |
264 |
| - return depthSum; |
265 |
| - }; |
266 |
| - const depth = maxDepth(nestedList); |
267 |
| - return dfs(nestedList, depth); |
| 441 | + for (const x of nestedList) { |
| 442 | + dfs(x, 1); |
| 443 | + } |
| 444 | + return (maxDepth + 1) * s - ws; |
268 | 445 | };
|
269 | 446 | ```
|
270 | 447 |
|
|
0 commit comments