How to Compute Nested List Weight Sum of Any Arrays?

  • 时间:2020-10-06 11:32:45
  • 分类:网络文摘
  • 阅读:109 次

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.

Example 1:
Input: [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1’s at depth 2, one 2 at depth 1.

Example 2:
Input: [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27.

This is the interface that allows for creating nested lists.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 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();
  }
 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();
  }

Depth First Search using Recursion

We can use Depth First Search (easy to implement using recursion) to iterate the list. If it is an integer, we update the sum with the value times depth, otherwise, it is a list, we can recursively call the function to get the partial sum.

In order to do this, we need a helper function which passes the current depth. When recursion calls finish/exit, the stack will restore the depth variable before it invokes the recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    int depthSum(vector<NestedInteger>& nestedList) {
        return helper(nestedList, 1);
    }
    
    int helper(vector<NestedInteger>& nestedList, int depth) {
        int r = 0;
        for (const auto &n: nestedList) {
            if (n.isInteger()) {
                r += n.getInteger() * depth;
            } else {
                r += helper((vector<NestedInteger> &)n.getList(), depth + 1); // depth first search - recursion
            }
        }
        return r;        
    }
};
class Solution {
public:
    int depthSum(vector<NestedInteger>& nestedList) {
        return helper(nestedList, 1);
    }
    
    int helper(vector<NestedInteger>& nestedList, int depth) {
        int r = 0;
        for (const auto &n: nestedList) {
            if (n.isInteger()) {
                r += n.getInteger() * depth;
            } else {
                r += helper((vector<NestedInteger> &)n.getList(), depth + 1); // depth first search - recursion
            }
        }
        return r;        
    }
};

The above algorithm to compute the nested weight list sum runs at O(N) time where N is the number of integers in the list including the sub-list as each integer is visited exactly once. In the worst case for inputs like [1, [1, [1, [1, ..] .. ] .. ] ..] There will be N depths, thus the space complexity will be O(N) via compiler-generated stacks through recursion.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
网站站长赚钱的6大好用的途径  整理6款站长赚钱方法 希望对你有所帮助  个人站长们常见的很多个网站盈利模式总结  春季饮食宜润肺,常吃炖梨既滋润又养人,口感甜香味道美  这道小学应用题比较难,解题关键是求相遇时间  豆腐搭配鸡蛋做出香酥可口的丸子,营养也很丰富  这道小学奥数题难倒多数学生,解题关键是比例  分享茄子的家常做法,吃起来不油腻,营养美味又下饭  中华人民共和国慈善法(主席令第四十三号)  中华人民共和国深海海底区域资源勘探开发法(主席令第四十二号) 
评论列表
添加评论