364. Nested List Weight Sum II

Given a nested list of integers, return the sum of all integers in the list weighted by their depth.

Each element is either an integer, or a list – whose elements may also be integers or other lists.

Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.

Example 1:

``````Input: [[1,1],2,[1,1]]
Output: 8
Explanation: Four 1's at depth 1, one 2 at depth 2.
``````

Example 2:

``````Input: [1,[4,[6]]]
Output: 17
Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17.
``````

[[1,1],2,[1,1]]：

1 1 1 1
2

[1,[4,[6]]]：

1
4
6

``````class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int res = 0;
vector<vector<int>> all;
for (auto &a : nestedList) {
helper(a, 0, all);
}
for (int i = (int)all.size() - 1; i >= 0; --i) {
for (int j = 0; j < all[i].size(); ++j) {
res += all[i][j] * ((int)all.size() - i);
}
}
return res;
}
void helper(NestedInteger& ni, int depth, vector<vector<int>>& all) {
vector<int> t;
if (depth < all.size()) t = all[depth];
else all.push_back(t);
if (ni.isInteger()) {
t.push_back(ni.getInteger());
all[depth] = t;
} else {
for (auto &a : ni.getList()) {
helper(a, depth + 1, all);
}
}
}
};
``````

``````class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int res = 0;
vector<int> v;
for (auto &a : nestedList) {
helper(a, 0, v);
}
for (int i = (int)v.size() - 1; i >= 0; --i) {
res += v[i] * ((int)v.size() - i);
}
return res;
}
void helper(NestedInteger& ni, int depth, vector<int>& v) {
if (depth >= v.size()) v.resize(depth + 1);
if (ni.isInteger()) {
v[depth] += ni.getInteger();
} else {
for (auto &a : ni.getList()) {
helper(a, depth + 1, v);
}
}
}
};
``````

``````class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int unweighted = 0, weighted = 0;
while (!nestedList.empty()) {
vector<NestedInteger> nextLevel;
for (auto a : nestedList) {
if (a.isInteger()) {
unweighted += a.getInteger();
} else {
nextLevel.insert(nextLevel.end(), a.getList().begin(), a.getList().end());
}
}
weighted += unweighted;
nestedList = nextLevel;
}
return weighted;
}
};
``````

``````class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int unweighted = 0, weighted = 0;
queue<vector<NestedInteger>> q;
q.push(nestedList);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
vector<NestedInteger> t = q.front(); q.pop();
for (auto a : t) {
if (a.isInteger()) unweighted += a.getInteger();
else if (!a.getList().empty()) q.push(a.getList());
}
}
weighted += unweighted;
}
return weighted;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/364

Nested List Weight Sum

Array Nesting

https://leetcode.com/problems/nested-list-weight-sum-ii/

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83655/JAVA-AC-BFS-solution

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83641/No-depth-variable-no-multiplication

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/114195/Java-one-pass-DFS-solution-mathematically

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83649/Share-my-2ms-intuitive-one-pass-no-multiplication-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation