package leetcode;

/**
 * https://leetcode.com/problems/surface-area-of-3d-shapes/
 */
public class Problem892 {
    public int surfaceArea(int[][] grid) {
        int result = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                result += 4 * grid[i][j];
                if (j - 1 >= 0) {
                    int diff = Math.abs(grid[i][j - 1] - grid[i][j]);
                    result -= 2 * diff;
                }
                if (i - 1 >= 0) {
                    int diff = Math.abs(grid[i - 1][j] - grid[i][j]);
                    result -= 2 * diff;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem892 prob = new Problem892();
//        System.out.println(prob.surfaceArea(new int[][]{
//            {1}
//        })); // 6
//        System.out.println(prob.surfaceArea(new int[][]{
//            {2}
//        })); // 10
//        System.out.println(prob.surfaceArea(new int[][]{
//            {3}
//        })); // 14
        System.out.println(prob.surfaceArea(new int[][]{
            {1, 2}, {3, 4}
        })); // 34
//        System.out.println(prob.surfaceArea(new int[][]{
//            {1, 1}, {1, 1}
//        })); // 16
//        System.out.println(prob.surfaceArea(new int[][]{
//            {1, 0}, {0, 2}
//        })); // 16
//        System.out.println(prob.surfaceArea(new int[][]{
//            {1, 1, 1}, {1, 0, 1}, {1, 1, 1}
//        })); // 32
//        System.out.println(prob.surfaceArea(new int[][]{
//            {2, 2, 2}, {2, 1, 2}, {2, 2, 2}
//        })); // 46
    }
}

@@ -302,17 +302,17 @@
 sult += 
-4
+6
  * grid%5B
@@ -342,91 +342,25 @@
 if (
-j - 1 %3E= 0) %7B%0A                    int diff = Math.abs(grid%5Bi%5D%5Bj - 1%5D - grid%5Bi%5D%5Bj%5D);
+grid%5Bi%5D%5Bj%5D %3E 1) %7B
 %0A   
@@ -382,36 +382,48 @@
   result -= 2 * 
-diff
+(grid%5Bi%5D%5Bj%5D - 1)
 ;%0A              
@@ -446,17 +446,17 @@
     if (
-i
+j
  - 1 %3E= 
@@ -488,20 +488,19 @@
 int 
-diff
+min
  = Math.
 abs(
@@ -499,28 +499,27 @@
 ath.
-abs
+min
 (grid%5Bi
+%5D%5Bj
  - 1%5D
-%5Bj%5D -
+,
  gri
@@ -566,12 +566,11 @@
 2 * 
-diff
+min
 ;%0A  
@@ -593,30 +593,24 @@
         
-    %7D%0A
         
 %7D%0A      
@@ -605,190 +605,26 @@
     
-%7D%0A        return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem892 prob = new Problem892();%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//
+if (i - 1 %3E= 0) %7B%0A
     
@@ -635,22 +635,16 @@
     
-%7B1%7D%0A//
         
 %7D));
@@ -643,786 +643,159 @@
     
-%7D)); // 6%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//            %7B2%7D%0A//        %7D)); // 10%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//            %7B3%7D%0A//        %7D)); // 14%0A        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A            %7B1, 2%7D, %7B3, 4%7D%0A        %7D)); // 34%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//            %7B1, 1%7D, %7B1, 1%7D%0A//        %7D)); // 16%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//            %7B1, 0%7D, %7B0, 2%7D%0A//        %7D)); // 16%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//
+int min = Math.min(grid%5Bi - 1%5D%5Bj%5D, grid%5Bi%5D%5Bj%5D);%0A                    result -= 2 * min;%0A         
        
+%7D%0A
      
-%7B1, 1, 1%7D, %7B1, 0, 1%7D, %7B1, 1, 1%7D%0A//        %7D)); // 32%0A//        System.out.println(prob.surfaceArea(new int%5B%5D%5B%5D%7B%0A//            %7B2, 2, 2%7D, %7B2, 1, 2%7D, %7B2, 2, 2%7D%0A//        %7D)); // 46
+       %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 892