算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable

算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable

Given a 2D matrix matrix, handle multiple queries of the following type:

  • Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Implement the NumMatrix class:

  • NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
  • int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Example 1:

www.zeeklog.com  - 算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable
Input
["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
[[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]
Output
[null, 8, 11, 12]

Explanation
NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 200
  • -105 <= matrix[i][j] <= 105
  • 0 <= row1 <= row2 < m
  • 0 <= col1 <= col2 < n
  • At most 10^4 calls will be made to sumRegion.

动态规划解法

www.zeeklog.com  - 算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable
class NumMatrix {
    int[][] sum;
    public NumMatrix(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        sum = new int[rows + 1][cols + 1];// sum[i][j] is sum of all elements inside the rectangle [0,0,i,j]
        for (int r = 1; r <= rows; r++) {
            for (int c = 1; c <= cols; c++) {
                sum[r][c] = sum[r - 1][c] + sum[r][c - 1] + matrix[r - 1][c - 1] - sum[r - 1][c - 1];
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
     	// Since our `sum` starts by 1 so we need to increase r1, c1, r2, c2 by 1
        return sum[row2 + 1][col2 + 1] - sum[row1][col2 + 1] - sum[row2 + 1][col1] + sum[row1][col1];
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

或者参考视频:https://youtu.be/PwDqpOMwg6U

www.zeeklog.com  - 算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable


www.zeeklog.com  - 算法: 求矩形框数字之和304. Range Sum Query 2D - Immutable

参考

https://leetcode.com/problems/range-sum-query-2d-immutable/discuss/572648/C%2B%2BJavaPython-Prefix-sum-with-Picture-explain-Clean-and-Concise

https://youtu.be/PwDqpOMwg6U

Read more

面试官问我Python日历模块,我直接用Flask开发Web版日历应用给他

面试官问我Python日历模块,我直接用Flask开发Web版日历应用给他

嚣张的面试开场 面试官:小伙子,python与时间有关的基础模块有哪些? 我: time datetime calendar 面试官:简单介绍下calendar日历模块 我: 我拒绝! 面试官:小子你很拽啊,那你想如何? 我:我想给大佬你做一个美观的网页版日历啊。 面试官:走两步,我瞧瞧... 我:好嘞! Python Calender模块 python的日历模块Calender提供了多种日历展示模式: 参数说明示例calendar.calendar(year)输出某一年的日历calendar.calendar(2019)monthcalendar(year, month)返回一个月中天数列表(不是当前月份的天数为0)calendar.monthcalendar(2019, 6)setfirstweekday(firstweekday)0是星期一,…,6为星期日calendar.setfirstweekday(firstweekday=6)prmonth(theyear, themonth,

By Ne0inhk
学富五车的你,敢迎战Python开发的成语接龙游戏吗?

学富五车的你,敢迎战Python开发的成语接龙游戏吗?

成语接龙 今天难得下班早,不用做公司的末班车,和同事乘公交回家。中途上来几个学生,相互在玩着成语接龙游戏。说是成语,但词汇却真是不堪入耳。 6月高考的前一天,我发布的一篇文章,,当时只是吧网站的成语爬下来保存到数据库中,文末提到有机会了抽时间拿这些数据搞点事情,那么今天就来搞事情吧。用3W+的成语数据库,开发一款成语接龙的小游戏。 接龙规则 成语接龙是中华民族传统的文字游戏。它不仅有着悠久的历史和广泛的社会基础,同时还是体现我国文字、文化、文明的一个缩影,是老少皆宜的民间文化娱乐活动。 成语接龙规则多样,大家一般熟知的是采用成语字头与字尾相连不断延伸的方法进行接龙;用四个字成语的最后一个字与下一句成语的第一个相同的字【音同就可以】,首尾相接不断延伸,形成长龙。 游戏演示 说了这么多,让我们来先睹为快,开一局接龙比赛吧: 游戏展示   既然是游戏,就得来个排名才有意思啊!之前测试了几轮数据,这次我们使用一个Neo的新用户来进行游戏,随便接龙了几次,然后我编了一个 “蝉鸣鸟叫”的成语结束这次演示,不然像我这种学富五车的有为青年,好好答不来个几十轮的哪里能结束啊,哈哈...可以

By Ne0inhk
python编译&反编译,你不知道的心机与陷阱

python编译&反编译,你不知道的心机与陷阱

python的文件后缀 谈到python的文件后缀,说眼花缭乱也不为过.来看看你遇到过哪些类型! .py 如果这个不知道,呵呵...那请出门左拐,你还是充钱那个少年,没有一丝丝改变。接着打游戏去吧... .pyc 这个后缀应该算是除了python的py代码外,遇到最多的一种文件类型了。虽然python被普遍认为是一种解释性语言,但谁说它就不能被编译后执行呢?python通过compile生成的pyc文件,然后由python的虚拟机执行。相对于py文件来说,编译成pyc本质上和py没有太大区别,只是对于这个模块的加载速度提高了,并没有提高代码的执行速度,通常情况下不用主动去编译pyc文件。那pyc文件存在的意义在哪里? 除了略微提高的模块加载速度外,更多的当然是为了一定意义上的保护源码不被泄露了。 当然为什么说一定意义?因为既然有编译,就毕竟存在反编译喽,这个一会儿说... 如何将py文件编译成pyc文件呢?方法有三: 1. 使用python直接编译 python -m sample.py 2. py_compile import py_compile py_c

By Ne0inhk
Python帮你在520俘获女友芳心

Python帮你在520俘获女友芳心

今天是个花钱的日子 今天是520,可预期到的是,估计有很多年轻的情侣们,已经为这个节日提前准备好久了吧?烂大街的套路无非就是送花、吃饭、电影院,看完电影找酒店。作为一个引爆消费的特别日子,程序猿们如何过节呢? 一行代码画爱心 这个骚操作不知道诱惑了多少人去学python,其实怎么说,如果真的代码写成那个样子,下班走夜路最好自带三级头,不然很容易挨闷棍。代码如下: print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<

By Ne0inhk