[C++] [Java] Clean Code


  • 1

    C++

    class Solution {
    public:
        int maxCount(int m, int n, vector<vector<int>>& ops) {
            for (auto op : ops) {
    //          if (op[0] == 0 || op[1] == 0)  continue;
                m = min(op[0], m);
                n = min(op[1], n);
            }
            return m * n;
        }
    };
    

    Java

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

    Because there is no test case for operation contains 0. Without the sanity check it still pass. In fact the test implementation behind is not checking this and returns 0 for customized test case like:

    3
    3
    [[2,2],[3,3], [3,0]]
    

    Update
    Thanks @sqfan pointing out this check is not needed:

    //          if (op[0] == 0 || op[1] == 0)  continue;
    

  • 1
    Z

    the note of problem has already mentioned The range of a is [1,m], and the range of b is [1,n].


  • 0

    @zqfan Thanks! Commentted out.


  • 0
    G

    @alexander yeah,you solution is good,we are almost same,this is my java 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;
        }
    }
    

Log in to reply
 

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