Simple java solution, beats 84%


  • 0
    Z

    public class Solution {
    public int depthSumInverse(List<NestedInteger> nestedList) {
    int height = getHeight(nestedList);

        int sum = getSum(nestedList , height);
        
        return sum;
    }
    
    public int getSum(List<NestedInteger> list , int weight) {
        int sum = 0;
        
        for(NestedInteger integer : list) {
            if(integer.isInteger()) {
                sum += weight * integer.getInteger();
            } else {
                sum += getSum(integer.getList() , weight - 1);
            }
        }
        
        return sum;
    }
    
    public int getHeight(List<NestedInteger> nestedList) {
        int height = 1;
        
        for(NestedInteger integer : nestedList) {
            if(!integer.isInteger()) {
                height = Math.max(height , 1 + getHeight(integer.getList()));
            }
        }
        
        return height;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.