package com.fishercoder.solutions;

import com.fishercoder.common.classes.NestedInteger;

import java.util.List;

public class _339 {
    class SolutionWithGlobalSum {
        private int sum = 0;

        public int depthSum(List<NestedInteger> nestedList) {
            return dfs(nestedList, 1);
        }

        private int dfs(List<NestedInteger> nestedList, int depth) {
            for (NestedInteger ni : nestedList) {
                if (ni.isInteger()) {
                    sum += depth * ni.getInteger();
                } else {
                    dfs(ni.getList(), depth + 1);
                }
            }
            return sum;
        }
    }

    class SolutionWithLocalSum {
        public int depthSum(List<NestedInteger> nestedList) {
            return dfs(nestedList, 1);
        }

        private int dfs(List<NestedInteger> nestedList, int depth) {
            int sum = 0;
            for (NestedInteger ni : nestedList) {
                sum += ni.isInteger() ? depth * ni.getInteger() : dfs(ni.getList(), depth + 1);
            }
            return sum;
        }
    }

}
@@ -129,17 +129,29 @@
 339 %7B%0A  
- 
+public static
  class S
@@ -161,28 +161,12 @@
 tion
-WithGlobalSum %7B%0A    
+1 %7B%0A
     
@@ -187,20 +187,16 @@
 m = 0;%0A%0A
-    
     publ
@@ -243,38 +243,32 @@
 edList) %7B%0A      
-    
-  
 return dfs(neste
@@ -278,35 +278,27 @@
 st, 1);%0A    
-    %7D%0A%0A    
+%7D%0A%0A
     private 
@@ -352,30 +352,24 @@
 th) %7B%0A      
-  
-    
 for (NestedI
@@ -390,32 +390,24 @@
 stedList) %7B%0A
-        
         if (
@@ -430,34 +430,24 @@
 %7B%0A          
- 
-         
 sum += depth
@@ -474,24 +474,16 @@
         
-   
-     
 %7D else %7B
@@ -483,26 +483,16 @@
  else %7B%0A
-          
         
@@ -535,38 +535,16 @@
     
-        %7D%0A            
 %7D%0A      
     
@@ -531,32 +531,34 @@
         %7D%0A      
+%7D%0A
       return sum
@@ -567,24 +567,30 @@
     
-   
- 
 %7D%0A
-  
   %7D%0A%0A  
- 
+public static
  cla
@@ -604,27 +604,12 @@
 tion
-WithLocalSum %7B%0A    
+2 %7B%0A
     
@@ -664,30 +664,24 @@
 st) %7B%0A      
-     
- 
 return dfs(n
@@ -699,27 +699,19 @@
 1);%0A    
-    %7D%0A%0A    
+%7D%0A%0A
     priv
@@ -773,22 +773,16 @@
 %7B%0A      
-    
-  
 int sum 
@@ -786,22 +786,16 @@
 um = 0;%0A
-      
       fo
@@ -838,24 +838,16 @@
         
-    
-    
 sum += n
@@ -928,28 +928,16 @@
     
-      %7D%0A    
+%7D%0A
       
-  
 retu
@@ -952,18 +952,12 @@
     
-    %7D%0A    %7D%0A%0A%7D
+%7D%0A  %7D%0A%7D%0A

refactor 339