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 }
  • 相关阅读:
    linux系统中如何查看日志 (转)
    php 获取随机字符串(原创)
    php Aes 128位算法
    linux 在线实验
    number随时间随机递增每天 不同 php(原创)
    php 判断字符串包含中文(转)
    同步,异步 阻塞,非阻塞, 异步+回调机制 线程队列 事件Event 丶协程
    线程的理论知识 开启线程的两种方式(Thread) 线程和进程之间的对比 线程的其他方法 守护进程 互斥锁 死锁现象,递归锁 信号量
    获取进程以及父进程的pid 验证进程之间的数据隔离 join方法 进程对象的其他属性 僵尸进程与孤儿进程(存在Linux系统中) 守护进程
    进程基础知识 操作系统 操作系统的发展史(多道技术) 进程介绍 python并发编程之:多进程
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9880836.html
Copyright © 2011-2022 走看看