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 [leetcode]339. Nested List Weight Sum嵌套列表加权和 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.

思路

跑的最快的方法是 DFS

1. We can observe that

1x + 2y + 3z  = (x+y+z) * (3+1)    -   (3x+2y+1z)

^ levelSum  ^maxDepth   ^ Nested List Weight Sum I problem

2. Use DFS recursion, converting this problem to Nested List Weight Sum I,  updating levelSum and maxPath at the same time when using DFS

代码

 class Solution {
int levelSum = 0;
int maxDepth = 1; public int depthSumInverse(List<NestedInteger> nestedList) {
int depthSum = dfs(nestedList, 1);
return levelSum * (maxDepth + 1) - depthSum;
} private int dfs(List<NestedInteger> nestedList, int depth) {
int sum = 0;
for (NestedInteger n : nestedList) {
if (n.isInteger()) {
// same as Nested List Weight Sum I
sum += n.getInteger() * depth;
// at the same time, use DFS to update levelSum and maxDepth
maxDepth = Math.max(depth, maxDepth);
levelSum += n.getInteger();
} else {
// same as Nested List Weight Sum I
sum += dfs(n.getList(), depth + 1);
}
}
return sum;
}
}

思路

最容易想到的方法 DFS

1. use helper function to get maxDepth

2. same as Nested List Weight Sum I, use dfs function to get result. Only concerning that do substraction instead of addition when entering next new level

 class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null || nestedList.size() == 0) return 0;
int depth = helper(nestedList);
int sum = dfs(nestedList, depth);
return sum;
}
// helper recursion function to get the maxDepth
public int helper(List<NestedInteger> nestedList) {
int depth = 0;
for (NestedInteger n : nestedList) {
if(n.isInteger()) {
depth = Math.max(depth, 1);
}
else {
depth = Math.max(depth, helper(n.getList()) + 1);
}
}
return depth;
} // same as Nested List Weight Sum I
public int dfs(List<NestedInteger> nestedList, int depth) {
int result = 0;
for (NestedInteger n : nestedList) {
if (n.isInteger()) {
result += n.getInteger() * depth;
} else {
result += dfs(n.getList(), depth - 1);
}
}
return result;
}
}

思路

BFS(level order traversal)

if we want to get 3x + 2y + 1z, we can use preSum tech like that

levelSum    x

preSum      x

result        x

=======================

levelSum    x     y

preSum      x    x+y

result         x    x + x + y

=======================

levelSum    x     y             z

preSum      x    x+y           x+y+z

result         x    x + x + y    x + x + y + x + y + z

代码

 class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null || nestedList.size() == 0) return 0;
// initialize
int preSum = 0;
int result = 0;
// put each item of list into the queue
Queue<NestedInteger> queue = new LinkedList<>(nestedList);
while(!queue.isEmpty()){
//depends on different depth, queue size is changeable
int size = queue.size();
int levelSum = 0;
for(int i = 0; i < size; i++){
NestedInteger n = queue.poll();
if(n.isInteger()){
levelSum += n.getInteger();
}
else{
// depends on different depth, queue size is changeable
queue.addAll(n.getList());
}
}
preSum += levelSum;
result += preSum;
}
return result;
}
}

[leetcode]364. Nested List Weight Sum II嵌套列表加权和II的更多相关文章

  1. LeetCode 339. Nested List Weight Sum (嵌套列表重和)$

    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

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

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

  5. [leetcode]339. Nested List Weight Sum嵌套列表加权和

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

  6. LeetCode 339. Nested List Weight Sum

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

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

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

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

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

  9. 364. Nested List Weight Sum II

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

随机推荐

  1. C# 调用打印机 打印 Excel

    打印 Excel 模板 大体思路,通过NPOI操作Excel文件,通过Spire将Excel转成图片,将图片传给系统打印. Spire是收费工具,在微软库中下载Free版本. #region 打印所用 ...

  2. python3中的编码

    python2字符串编码存在的问题: 使用 ASCII 码作为默认编码方式,对中文处理不友好 把字符串分为 unicode 和 str 两种类型,将unicode作为唯一内码,误导开发者 python ...

  3. ubuntu-docker入门到放弃(七)操作系统

    操作系统相信很多人都会装,但是当使用docker容器来安装操作系统的时候,还是跟我们平时安装操作系统有很大区别的,我们之前也下载安装过centos系统,你会发现跟我们之前的操作系统相比,很精简,那么我 ...

  4. Win7系统安装Centos7.0双系统(一)

    项目环境测试,过去在虚拟机安装只要配置好镜像很快就可以轻松安装,但是在真实机中安装就有些略坑.网上有很多说法是把iso文件中安装引导拷出来,试了很久还是觉得U盘镜像直接安装更便捷.靠谱.因为CentO ...

  5. springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml

    springMVC注解:@Controller @Service @Repository 分别标注于web层,service层,dao层. web.xml <?xml version=" ...

  6. 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络

    基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...

  7. WPF Binding Mode,UpdateSourceTrigger

    WPF 绑定模式(mode) 枚举值有5个1:OneWay(源变就更新目标属性)2:TwoWay(源变就更新目标并且目标变就更新源)3:OneTime(只根据源来设置目标,以后都不会变)4:OneWa ...

  8. JS-Promise笔记

    转自:http://www.runoob.com/w3cnote/javascript-promise-object.html ECMAscript 6 原生提供了 Promise 对象. Promi ...

  9. 权限管理demo-Http请求前后监听工具

    工具作用: 1. 输出每次请求的参数 2. 接口的请求时间 package com.mmall.common; import com.mmall.util.JsonMapper; import lom ...

  10. thinkphp5 与 endroid 二维码生成

    windows compser安装endroid/qrcode,自己安装好composer工具; 1. 项目目录 文件 composer.json require 里添加 "endroid/ ...