Range Addition II


  • 0

    Click here to see the full article post


  • 0

    hi @vinod23, in Approach #2, for clarification, i think it's good to create the minX and minY variables, and not to reuse m and n :)

    class Solution {
        func maxCount(_ m: Int, _ n: Int, _ ops: [[Int]]) -> Int {
            var minX = m
            var minY = n
            
            for op in ops {
                minX = min(op[0], minX)
                minY = min(op[1], minY)
            }
    
            return minX * minY
        }
    }
    

  • 0
    S

    We can also add this solution as something that was accepted, but not better. As this has the time complexity of x*(m + n).

    class Solution {
        public int maxCount(int m, int n, int[][] ops) {
            int[] rows = new int[m];
            int[] cols = new int[n];
            int max = 0;
                 
            for(int[] op: ops) {
            	for(int i = 0; i < op[0]; ++i) {
            		rows[i]++;
            		if(max < rows[i]) max = rows[i];
            	}
            	
            	for(int j = 0; j < op[1]; ++j) {
            		cols[j]++;
            		if(max < cols[j]) max = cols[j];
            	}
            }
            
            int rowCount = 0;
            int colCount = 0;
            for(int i = 0; i < m; ++i) {
            	if(rows[i] == max) rowCount++;
            }
            
            for(int i = 0; i < n; ++i) {
            	if(cols[i] == max) colCount++;
            }
            return rowCount * colCount;
        }
    }
    

Log in to reply
 

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