Java Solution, find Min


  • 12
    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;
        }
    }
    

  • 8

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


  • 0
    L
    This post is deleted!

  • 2
    G

    @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;
        }
    }
    

  • 0
    S

    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;
    }

  • 0

    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;

  • 0

    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;
    }
    

  • 0
    L

    Great solution!


  • 2

    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;
        }
    

  • 0
    K

    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!


Log in to reply
 

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