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. ...
随机推荐
- Mybatis @ResultMap复用@Result
@ResultMap复用@Result: 可以简写成:@ResultMap("userMap")
- [CodeChef-ANUDTQ] Dynamic Trees and Queries
类似维护括号序列,给每个点建两个点,然后所有操作都能轻松支持了.注意sum和lastans是long long. #include<cstdio> #include<algorith ...
- go语言学习---使用os.Args获取简单参数(命令行解析)
实例1: //main package main import ( "fmt" "os" ) func main() { fmt.Println(os.Args ...
- harbor helm仓库使用
harbor helm仓库使用 官方文档地址:https://github.com/goharbor/harbor Monocular 从1.0 开始专注于helm 的UI展示,对于部署以及维护已经去 ...
- CentOS7 修改Jenkins以root用户运行
修改Jenkins以root用户运行,这在正式环境中是不可取的,但在自己的测试环境中就无所谓了啦,怎么方便怎么来. 1. 修改Jenkins配置文件1.1 修改$JENKINS_USER打开jenki ...
- Linux 系统中如何进入退出 vim 编辑器
在 Linux 中,vim 编辑器是系统自带的文本编辑器,但要修改某个文本文件,可不是像 Windows 那样操作,更有新手,进入 vi 编辑器后,无法退出以致于强制关机,其实,这个vim(vi)也是 ...
- 我与SAP成都研究院吴院长的二三事
这几天Jerry没怎么看手机,今天才注意到,昨天SAP中国研究院公众号上发布了一篇文章:SAP高管说: 体验经济时代下的SAP客户体验.仔细一看,这不是咱SAP成都研究院的吴院长么. 在今年没有发生部 ...
- mysql基础指令知识
桌面指令(cmd)进入mysql客户端 第一步:安装mysql,配置环境变量 第二步:手动开启服务 第三步:输入如下指令: mysql [-h localhost -P 3306] -u 用户名 - ...
- c# Directory类的常用方法
- hive 外部表和内部表的区别和相互转换
Hive内部表和外部表区别 1.创建内部表时,内部表的数据文件是保存在指定的路径的:如若创建外部表,则只记录数据所在的路径,不会对数据位置做改变. 2.删除表的时候,内部表元数据和数据会跟着一起删除. ...