原题链接在这里: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.

LeetCode 364. Nested List Weight Sum II的更多相关文章

  1. [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. ...

  2. [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. ...

  3. [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. ...

  4. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  5. 364. Nested List Weight Sum II 大小反向的括号加权求和

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

  6. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...

  7. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  8. [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. ...

  9. 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. ...

随机推荐

  1. c++ map容器使用及问题

    C++ STL库map容器一些总结,欢迎大家指正补充. map容器由两部分组成,分别为关键字(Key)和值(Value),关键字和值都可以声明为任意类型的数据,注意:关键字唯一,不能重复!使用需包含头 ...

  2. kubernetes 实践二:kubectl命令使用

    这里记录kubernetes学习和使用过程中的内容. CentOS7 k8s-1.13 flanneld-0.10 docker-18.06 etcd-3.3 kubectl用法概述 kubectl是 ...

  3. html2canvas以及domtoimage的使用踩坑总结

    前言 首先做个自我介绍,我是成都某企业的一名刚刚入行约一年的前端,在之前的开发过程中,遇到了问题,也解决了问题,但是在下一次解决相同问题的时候,只对这个问题有一丝丝的印象,还需要从新去查找,于是,我注 ...

  4. [CF436D]Pudding Monsters

    题目大意:有一个长度为$2\times 10^5$的板,有$n(n\leqslant 10^5)$个格子$a_1,\dots,a_n$有布丁怪兽,一开始连续的怪兽算一个怪兽,有$m(m\leqslan ...

  5. NOI2017

    整数(线段树) 不难想到按位处理,位数比较多考虑使用动态开点线段树维护大数,那么复杂度是\(O(nlog^2n)\)的,不够优秀. 但注意到我们需要支持的是二进制下的加减法,而在二进制下我们可以使用i ...

  6. 深度学习 Bottleneck layer / Bottleneck feature

    最近在学习deeplearning的时候接触到了bottle-neck layer,好奇它的作用于是便扒了一些论文(论文链接放在文末吧),系统的了解一下bottle-neck feature究竟有什么 ...

  7. 让table中的td不会被过长的文字撑开,并且自动出现省略号

    <style type="text/css"> table {width:600px;table-layout:fixed;} td {white-space:nowr ...

  8. OO第三单元(地铁,JML)单元总结

    OO第三单元(地铁,JML)单元总结 这是我们OO课程的第二个单元,这个单元的主要目的是让我们熟悉并了解JML来是我们具有规格化编程架构的思想.这个单元的主题一开始并不明了,从第一次作业的路径到第二次 ...

  9. dubbo源码阅读之负载均衡

    负载均衡 在之前集群的文章中,我们分析了通过监听注册中心可以获取到多个服务提供者,并创建多个Invoker,然后通过集群类如FailoverClusterInvoker将多个Invoker封装在一起, ...

  10. Redis持久化小结

    RDB RDB持久化方式是通过快照(snapshotting)完成的,当符合一定条件时,Redis将内存中所有数据以二进制方式生成一份副本并存储在硬盘上. 触发机制 save命令:阻塞当前Redis服 ...