# Java Solution, find Min

• ``````public class Solution {
public int maxCount(int m, int n, int[][] ops) {
if (ops == null || ops.length == 0) {
return m * n;
}

int row = Integer.MAX_VALUE, col = Integer.MAX_VALUE;
for(int[] op : ops) {
row = Math.min(row, op[0]);
col = Math.min(col, op[1]);
}

return row * col;
}
}
``````

• You don't need the first `if` statement if you init `row = m` and `col = n` :)

• This post is deleted!

• @shawngao well,actually,you don't need to judge whether ops is null or...,just define your row=m,col=n.this is my code:

``````public class Solution {
public int maxCount(int m, int n, int[][] ops) {
for(int i=0;i<ops.length;i++){
if(ops[i][0]<m) m=ops[i][0];
if(ops[i][1]<n) n=ops[i][1];
}
return m*n;
}
}
``````

• Same idea :)

``````public int maxCount(int m, int n, int[][] ops) {
int amin = m;
int bmin = n;
for(int i=0; i<ops.length; i++){
amin = Math.min(amin, ops[i][0]);
bmin = Math.min(bmin, ops[i][1]);
}

return amin*bmin;
}``````

• Find the minimum x, y.

``````      int x = m, y = n;
for(int[] op: ops){
x = Math.min(x, op[0]);
y = Math.min(y, op[1]);
}
return x * y;``````

• I also did it same way.

``````public int maxCount(int m, int n, int[][] ops) {
int minx = m, miny = n;
for (int [] op : ops) { minx = Math.min (minx, op [0]); miny = Math.min (miny, op [1]); }
return minx * miny;
}
``````

• Great solution!

• Same idea. btw, this is an awful problem. I did not see any interesting link between the Array and matrix..

``````    public int maxCount(int m, int n, int[][] ops) {
if (ops == null || ops.length == 0) return m * n;
int minRow = ops[0][0];
int minCol = ops[0][1];
for (int i = 1; i < ops.length; i++) {
minRow = Math.min(ops[i][0], minRow);
minCol = Math.min(ops[i][1], minCol);
}
return minRow * minCol;
}
``````

• Can someone explain the 'trick' to this question, because I am just not getting it. What's the connection between the minimum of the row/column length and the number of operations?

I wrote code that creates the matrix and applies the operations to it, and tracks the max integer and how often it is seen as it goes along- that produces the correct answer. But it runs out memory in some of the test cases, so that naive approach won't work. But I can't make the logical jump to this shortcut method!

• @karlr42 The trick is to realize that the first min(a) rows are updated by every operation. Similarly the first min(b) columns are updated by every operation. So that sub matrix from 0 to mina, 0 to minb are updated by every operation. So those cells contain the max, and we simply count the number of those cells.

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