package leetcode;

/**
 * https://leetcode.com/problems/range-addition-ii/
 */
public class Problem598 {
    public int maxCount(int m, int n, int[][] ops) {
        if (ops.length == 0) {
            return m * n;
        }
        int[][] matrix = new int[m][n];
        int max = 0;
        int count = 0;
        for (int x = 0; x < ops.length; x++) {
            int[] op = ops[x];
            for (int i = 0; i < op[0]; i++) {
                for (int j = 0; j < op[1]; j++) {
                    matrix[i][j]++;
                    if (max < matrix[i][j]) {
                        max = matrix[i][j];
                        count = 1;
                    } else if (max == matrix[i][j]) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Problem598 prob = new Problem598();
//        System.out.println(prob.maxCount(3, 3, new int[][]{
//            {2, 2},
//            {3, 3}
//        })); // 4
//        System.out.println(prob.maxCount(3, 3, new int[][]{
//            {2, 1},
//            {1, 3}
//        })); // 1
//        System.out.println(prob.maxCount(3, 3, new int[][]{
//        })); // 9
        System.out.println(prob.maxCount(6, 3, new int[][]{
            {2, 2}, {5, 2}, {5, 1}
        })); // 2
//        System.out.println(prob.maxCount(39999, 39999, new int[][]{
//            {19999,  19999}
//        })); // 399960001
    }
}

@@ -233,52 +233,16 @@
  int
-%5B%5D%5B%5D matrix = new int%5Bm%5D%5Bn%5D;%0A        int max
+ rowSize
  = 0
@@ -257,19 +257,21 @@
   int co
-unt
+lSize
  = 0;%0A  
@@ -289,16 +289,16 @@
 int 
-x
+i
  = 0; 
-x
+i
  %3C o
@@ -308,17 +308,17 @@
 length; 
-x
+i
 ++) %7B%0A  
@@ -332,68 +332,17 @@
    i
-nt%5B%5D op = ops%5Bx%5D;%0A            for (int 
+f (
 i =
+=
  0
-; i %3C op%5B0%5D; i++
 ) %7B%0A
@@ -361,165 +361,63 @@
     
-for (int j = 0; j %3C op%5B1%5D; j++) %7B%0A                    matrix%5Bi%5D%5Bj%5D++;%0A                    if (max %3C matrix%5Bi%5D%5Bj%5D) %7B%0A                        max = matrix
+rowSize = ops%5Bi%5D%5B0%5D;%0A                colSize = ops
 %5Bi%5D%5B
-j
+1
 %5D;%0A 
@@ -431,872 +431,186 @@
     
-            count = 1;%0A                    %7D else if (max == matrix%5Bi%5D%5Bj%5D) %7B%0A                        count++;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return count;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem598 prob = new Problem598();%0A//        System.out.println(prob.maxCount(3, 3, new int%5B%5D%5B%5D%7B%0A//            %7B2, 2%7D,%0A//            %7B3, 3%7D%0A//        %7D)); // 4%0A//        System.out.println(prob.maxCount(3, 3, new int%5B%5D%5B%5D%7B%0A//            %7B2, 1%7D,%0A//            %7B1, 3%7D%0A//        %7D)); // 1%0A//        System.out.println(prob.maxCount(3, 3, new int%5B%5D%5B%5D%7B%0A//        %7D)); // 9%0A        System.out.println(prob.maxCount(6, 3, new int%5B%5D%5B%5D%7B%0A            %7B2, 2%7D, %7B5, 2%7D, %7B5, 1%7D%0A        %7D)); // 2%0A//        System.out.println(prob.maxCount(39999, 39999, new int%5B%5D%5B%5D%7B%0A//            %7B19999,  19999%7D%0A//        %7D)); // 399960001
+%7D else %7B%0A                rowSize = Math.min(rowSize, ops%5Bi%5D%5B0%5D);%0A                colSize = Math.min(colSize, ops%5Bi%5D%5B1%5D);%0A            %7D%0A        %7D%0A        return rowSize * colSize;
 %0A   

Solve problem 598