Sunday, June 25, 2017

598. Range Addition II

Say the operations are [(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]. The top left square is clearly incremented by every operation. If some square (x, y) has x >= x_i, then it will not be marked by operation i. So all squares (x, y) with x >= min_i(x_i) do not get marked.
Thus, when there is atleast one operation, all squares (x, y) with 0 <= x < min(x_1, x_2, ..., x_n) and 0 <= y < min(y_1, y_2, ..., y_n) get marked; and there are min_i(x_i) * min_i(y_i) of them. If there are no operations, then what is marked is the entire board.
39.07 %
public class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        int rowMin = m;
        int colMin = n;
        for (int[] op : ops) {
            rowMin = Math.min(rowMin, op[0]);
            colMin = Math.min(colMin, op[1]);
        }
        return rowMin * colMin;
    }
}

No comments:

Post a Comment