zoukankan      html  css  js  c++  java
  • [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.

    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

    代码

     1 class Solution {
     2     int levelSum = 0;
     3     int maxDepth = 1;
     4     
     5     public int depthSumInverse(List<NestedInteger> nestedList) {
     6         int depthSum = dfs(nestedList, 1);
     7         return levelSum * (maxDepth + 1) - depthSum;
     8     }
     9     
    10     private int dfs(List<NestedInteger> nestedList, int depth) {
    11         int sum = 0;
    12         for (NestedInteger n : nestedList) {
    13             if (n.isInteger()) {
    14                 // same as Nested List Weight Sum I 
    15                 sum += n.getInteger() * depth;
    16                 // at the same time, use DFS to update levelSum and maxDepth
    17                 maxDepth = Math.max(depth, maxDepth);
    18                 levelSum += n.getInteger();
    19             } else {
    20                 // same as Nested List Weight Sum I 
    21                 sum += dfs(n.getList(), depth + 1);
    22             }
    23         }
    24         return sum;
    25     }
    26 }

    思路

    最容易想到的方法 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

     1 class Solution {
     2     public int depthSumInverse(List<NestedInteger> nestedList) {
     3         // corner case
     4         if(nestedList == null || nestedList.size() == 0) return 0;
     5         int depth = helper(nestedList);
     6         int sum = dfs(nestedList, depth);
     7         return sum;
     8     }
     9     // helper recursion function to get the maxDepth
    10     public int helper(List<NestedInteger> nestedList) {
    11         int depth = 0;
    12         for (NestedInteger n : nestedList) {
    13             if(n.isInteger()) {
    14                depth = Math.max(depth, 1); 
    15             }
    16             else {
    17                 depth = Math.max(depth, helper(n.getList()) + 1);
    18             }
    19         }
    20         return depth;
    21     }
    22     
    23     // same as Nested List Weight Sum I
    24     public int dfs(List<NestedInteger> nestedList, int depth) {
    25         int result = 0;
    26         for (NestedInteger n : nestedList) {
    27             if (n.isInteger()) {
    28                 result += n.getInteger() * depth;
    29             } else {
    30                 result += dfs(n.getList(), depth - 1);
    31             }
    32         }
    33         return result;
    34     }
    35 }

    思路

    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 

    代码

     1 class Solution {
     2     public int depthSumInverse(List<NestedInteger> nestedList) {
     3         // corner case
     4         if(nestedList == null || nestedList.size() == 0) return 0;
     5          // initialize
     6         int preSum = 0;
     7         int result = 0;
     8         // put each item of list into the queue
     9         Queue<NestedInteger> queue = new LinkedList<>(nestedList);   
    10         while(!queue.isEmpty()){
    11             //depends on different depth, queue size is changeable
    12             int size = queue.size();
    13             int levelSum = 0;
    14             for(int i = 0; i < size; i++){
    15                 NestedInteger n = queue.poll();
    16                 if(n.isInteger()){
    17                     levelSum += n.getInteger();
    18                 }
    19                 else{
    20                     // depends on different depth, queue size is changeable
    21                     queue.addAll(n.getList());
    22                 }
    23             }
    24             preSum += levelSum;
    25             result += preSum;
    26         }
    27         return result; 
    28     }
    29 }
  • 相关阅读:
    Devrama Slider
    全栈开发必备的10款 Sublime Text 插件
    经典网页设计:使用颜色滤镜效果的20个网站
    Nibbler – 免费的网站测试和指标评分工具
    使用 HTML5 Canvas 绘制出惊艳的水滴效果
    Qt4 和 Qt5 模块的分类
    设计Qt风格的C++API
    Qt属性系统
    Qt实现艺术字效果
    Qt中容器类应该如何存储对象(最好使用对象指针类型,如:QList<TestObj*>,而不要使用 QList<TestObj> 这样的定义,建议采用 智能指针QSharedPointer)
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9880836.html
Copyright © 2011-2022 走看看