---
# System prepended metadata

title: '20191219'

---

# 20191219

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Your algorithm should run in O(n) complexity.

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

Input: [250, 251, 3, 4, 5, 6, 7, 211]
Output: 5

// 100, 4, 200, 1, 3, 2

```
public int longestConsecutive(int[] nums) {
    HashSet<Integer> set = new HashSet<>();
    for(int num: nums) {
        set.add(num);
    }

    int result = 0;

    for (int num : nums) {
        int count = 1;

        int down = num - 1; // 99
        while (set.contains(down)) {
            set.remove(down);
            down--;
            count++;
        }

        int up = num + 1;
        while (set.contains(up)) {
            set.remove(up);
            up++;
            count++;
        }

        result = Math.max(result, count);
    }

    return result;
}
```

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.
[1 * 2 + 1 * 2]
[2 * 1]
[1 * 2 + 1 * 2]
10

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.
1 * 1 
4 * 2
6 * 3 
27
```
/**
 * // 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 depthSum(List<NestedInteger> nestedList) {
        return dfs(nestedList, 1);
    }
    
    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++);
            }
        }
        return result;
    }
}
```
```
class Solution {
    public int depthSum(List<NestedInteger> nestedList) {
        if (nestedList == null) return 0;
        
        int sum = 0;
        int level = 1;
        
        Queue<NestedInteger> queue = new LinkedList<>();
        for (NestedInteger n : nestedList) {
            queue.offer(n);
        }
        while (!queue.isEmpty()) {
            int size = queue.size();
            
            for (int i = 0; i < size; i++) {
                NestedInteger n = queue.poll;
                
                if (n.isInteger()) {
                    sum += n.getInteger() * level;
                } else {
                    queue.addAll(n.getList());
                }
            }
            
            level++;
        }
        
        return sum;
    }
}
```

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

```
/**
 * // 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) return 0;
        
        // Input: [1,[4,[6]]]

        Queue<NestedInteger> queue = new LinkedList<>();
        for (NestedInteger n : nestedList) {
            queue.offer(n);
        }
        int prev = 0;
        int sum = 0;
        
                2
               / \
            [1,1][1,1]   
        
        // [[1,1],2,[1,1]]
        while (!queue.isEmpty()) {
            int size = queue.size(); // 3
            int levelSum = 0;
            for (int i = 0; i < size; i++) {
                NestedInteger n = queue.poll(); // 6
                if (n.isInteger()) {
                    levelSum += n.getInteger(); // 6
                }
                List<NestedInteger> nextList = n.getList();
                if (nextList != null) {
                    for (NestedInteger nn : nextList) {
                        queue.offer(nn); // [6]
                    }
                }
            }
            prev += levelSum; // 1 -> 5 -> 11
            sum += prev; // 1 -> 6 -> 17
        }
        
        return sum;
    }
}
```