原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/

题目:

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:
Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2)

Example 2:
Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)

题解:

The question is asking for weight sum. The top level has largest depth.

Thus the top level need to be added maximum depth times.

We could accumlate the sum of Integer of the current level, add it to the res.

For next level, we could continue adding Integer to the same sum, add it to the res. Then upper level sum is added multiple times.

Time Complexity: O(n+m). n 是flat后一共有多少个数字, 就像BFS的node数. m 是层数,就像BFS的edge数.

Space: O(n).

AC Java:

 /**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
int weight = 0;
int unweight = 0;
while(!nestedList.isEmpty()){
List<NestedInteger> nextList = new ArrayList<NestedInteger>();
for(NestedInteger item : nestedList){
if(item.isInteger()){
unweight += item.getInteger();
}else{
nextList.addAll(item.getList());
}
}
weight += unweight;
nestedList = nextList;
}
return weight;
}
}

Could also do DFS.

DFS return value needs both maximum depth and accumlated sum.

It does DFS bottom-up.

Time Complexity: O(n+m).

Space: O(n).

AC Java:

 /**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if(nestedList == null || nestedList.size() == 0){
return 0;
} int [] res = dfs(nestedList);
return res[1];
} private int [] dfs(List<NestedInteger> nestedList){
if(nestedList == null || nestedList.size() == 0){
return new int[]{0, 0};
} int levelSum = 0;
List<NestedInteger> nexts = new ArrayList<>();
for(NestedInteger ni : nestedList){
if(ni.isInteger()){
levelSum += ni.getInteger();
}else{
nexts.addAll(ni.getList());
}
} int [] arr = dfs(nexts);
return new int[]{arr[0]+1, levelSum*(arr[0]+1)+arr[1]};
}
}

类似Nested List Weight Sum.

最新文章

  1. C语言运算符优先级和口诀(转)
  2. Region Representaion and Description
  3. Unity3d 枚举某个目录下所有资源
  4. 纯JavaScripst的全选、全不选、反选 【转】
  5. wamp链接mysql数据库
  6. linux下导入、导出mysql数据库命令 下载文件到本地
  7. WINDOWS之入侵痕迹清理总结
  8. Eclipse的模板设置代码
  9. perl 对象
  10. ffmpeg参数解释 &lt;第三篇&gt;
  11. xmu1214: 购物
  12. 模拟Hibernate框架的小demo
  13. Determine whether an integer is a palindrome. Do this without extra space.
  14. HttpWebRequest类库注意事项以及常见问题
  15. WebClient下载文件
  16. CMDB资产管理系统开发【day26】:admin action
  17. C++中的纯虚方法
  18. VS下载Github的项目引用报错
  19. LINQ之路15:LINQ Operators之元素运算符、集合方法、量词方法
  20. MAC shell ps 命令详解(转)

热门文章

  1. 下载安装GO,编辑器GOLand和在GOLand上运行项目的几种方式
  2. pandas再次学习
  3. PAT(B) 1045 快速排序(C)
  4. PHP生成随机单词
  5. 阅读笔记——《How a Facebook rejection pushed me to start and grow a profitable business in 12 months》
  6. 「APIO2016」烟花表演
  7. Spring AOP创建BeforeAdvice和AfterAdvice实例
  8. IOC+EF+Core项目搭建EF封装(一)
  9. leetcode 数组
  10. Java 之 数据库连接池