zoukankan      html  css  js  c++  java
  • 598. Range Addition II 矩阵的范围叠加

    [抄题]:

    Given an m * n matrix M initialized with all 0's and several update operations.

    Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.

    You need to count and return the number of maximum integers in the matrix after performing all the operations.

    Example 1:

    Input: 
    m = 3, n = 3
    operations = [[2,2],[3,3]]
    Output: 4
    Explanation: 
    Initially, M = 
    [[0, 0, 0],
     [0, 0, 0],
     [0, 0, 0]]
    
    After performing [2,2], M = 
    [[1, 1, 0],
     [1, 1, 0],
     [0, 0, 0]]
    
    After performing [3,3], M = 
    [[2, 2, 1],
     [2, 2, 1],
     [1, 1, 1]]
    
    So the maximum integer in M is 2, and there are four of it in M. So return 4.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [一句话思路]:

    行列都取最

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    [复杂度]:Time complexity: O() Space complexity: O()

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    表示每次取出的是数组,op[0]表示该被取出的数组中的第0位,op[1]表示该数组中的第1位

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

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

    370. Range Addition 就是操作数组吧

     [代码风格] :

    class Solution {
        public int maxCount(int m, int n, int[][] ops) {
            //cc
            if (ops == null || ops.length == 0) return m * n;
            
            //ini:min max
            int row = Integer.MAX_VALUE, col = Integer.MAX_VALUE;
            
            //find min, max
            for (int[] op : ops) {
                row = Math.min(op[0], row);
                col = Math.min(op[1], col);
            }
            
            return row * col;
        }
    }
    View Code
  • 相关阅读:
    开放个人电脑端口[Windows]
    mysql局域网服务搭建
    JS数据分组[JSON]
    网站缓存【我只是单纯的保存网站!】
    JS生成数字加减乘法验证码
    【http反向代理】多个域名指向同一个ip的不同网站解决方法
    JQuery获取ID含有特殊字符的DOM元素
    java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    大型网站技术架构,6网站的伸缩性架构之分布式缓存集群的伸缩性设计
    大型网站技术架构,6网站的伸缩性架构之应用服务器集群的伸缩性设计
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8987669.html
Copyright © 2011-2022 走看看