LeetCode 364. Nested List Weight Sum II
原题链接在这里: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]};
}
}
LeetCode 364. Nested List Weight Sum II的更多相关文章
- [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 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. ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 364. Nested List Weight Sum II 大小反向的括号加权求和
[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...
- 364. Nested List Weight Sum II
这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- [LeetCode] Nested List Weight Sum II 嵌套链表权重和之二
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- Leetcode: Nested List Weight Sum II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- Apache Kafka Consumer 消费者集
1.目标 在我们的上一篇文章中,我们讨论了Kafka Producer.今天,我们将讨论Kafka Consumer.首先,我们将看到什么是Kafka Consumer和Kafka Consumer的 ...
- KAFA优点和缺点
我们上一期的Kafka教程中,我们讨论了Kafka的Books.今天,我们将讨论卡夫卡的优势和劣势.因为,在使用之前了解任何技术的局限性非常重要,在优点的情况下也是如此.所以,让我们详细讨论卡夫卡优势 ...
- bootstrap.min.css.map作用
我先说一下什么是source map文件. source map文件是js文件压缩后,文件的变量名替换对应.变量所在位置等元信息数据文件,一般这种文件和min.js主文件放在同一个目录下. 比如压缩后 ...
- (七)linux 学习 -- 键盘高级操作技巧
The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap09.html 文章目录 移动光标 修改文本 ...
- [转]mongodb authentication 设置权限之后,新建个管理账户和一般数据库用户,在win 7 64bit 环境下测试使用实例
如果之前安装mongodb时没有使用 --auth,那么必须要卸载MongoDB服务,进行重新安装,设置账号权限才生效! 主要是解决在测试使用mongo db 时候,总是出现的MongoAuthent ...
- jdk安装以及Java环境配置
jdk其实自己大一的时候就已经装过,java环境也配置过,但是随着后面学习的东西越来越多,要安装的软件也越来越多,一开始没有安装路径的概念,好多东西都放的很乱.接着这次自己复习java的机会,于是重新 ...
- Sqlite in flutter, how database assets work
First off, you will need to construct a sqlite database from your csv. This can be done in the follo ...
- 二叉树、B树、B+树、B*树、VAL树、红黑树
二叉搜索树 每个节点只存储一个关键字, 每个节点最多有两个子节点, 左子节点存储的关键字小于本节点存储的关键字 右子节点存储的关键字大于本节点存储的关键字 搜索时,从根节点开始搜索,小于走左结点,大于 ...
- vue实现滑块滑动校验
为了防止机器操作自动提交,我们需要添加滑动校验. 实现代码如下: 1.子组件slider.vue <template> <div class="drag" r ...
- vue标签内循环数据逗号分隔
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...