Java solution with explanation


  • 0
    T
    public class Solution {
        /*We need to find the intersection of those input operations.
        * One trick of this problem is that the lower bounds of each operation for i and j are 0.
        * When a or b equals to 0, no element should be added by 1.
        * We need to find the smallest positive value a and b so that the answer would be a*b.
        */
        public int maxCount(int m, int n, int[][] ops) {
            int smallestA = Integer.MAX_VALUE;
            int smallestB = Integer.MAX_VALUE;
            for(int[] op : ops){
                if (op[0] > 0 && smallestA > op[0])
                    smallestA = op[0];
                if (op[1] > 0 && smallestB > op[1])
                    smallestB = op[1];
            }
            if(smallestA > m) smallestA = m;
            if(smallestB > n) smallestB = n;
            return smallestA * smallestB;
        }
    }
    

Log in to reply
 

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