Description

Given an array of n * m matrix, and a moving matrix window (size k * k), move the window from top left to bottom right at each iteration, find the maximum sum inside the window at each moving.

Return 0 if the answer does not exist.

Example

Example 1:

Input:[[1,5,3],[3,2,1],[4,1,9]],k=2
Output:13
Explanation:
At first the window is at the start of the matrix like this [
[|1, 5|, 3],
[|3, 2|, 1],
[4, 1, 9],
]
,get the sum 11;
then the window move one step forward. [
[1, |5, 3|],
[3, |2, 1|],
[4, 1, 9],
]
,get the sum 11;
then the window move one step forward again. [
[1, 5, 3],
[|3, 2|, 1],
[|4, 1|, 9],
]
,get the sum 10;
then the window move one step forward again. [
[1, 5, 3],
[3, |2, 1|],
[4, |1, 9|],
]
,get the sum 13;
SO finally, get the maximum from all the sum which is 13.

Example 2:

Input:[[10],k=1
Output:10
Explanation:
sliding window size is 1*1,and return 10.

Challenge

O(n^2) time.

思路:

考点:

  • 二维前缀和

题解:

  • sum[i][j]存储左上角坐标为(0,0),右下角坐标为(i,j)的子矩阵的和。
  • sum[i][j] = matrix[i - 1][j - 1] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];递推求值即可,两部分相加,减去重复计算部分。
  • int value = sum[i][j] - sum[i - k][j] -sum[i][j - k] + sum[i - k][j - k];可求得一个k * k大小子矩阵的和。
    public class Solution {
    /**
    * @param matrix: an integer array of n * m matrix
    * @param k: An integer
    * @return: the maximum number
    */
    public int maxSlidingMatrix(int[][] matrix, int k) {
    // Write your code here
    int n = matrix.length;
    if (n == 0 || n < k)
    return 0;
    int m = matrix[0].length;
    if (m == 0 || m < k)
    return 0; int[][] sum = new int[n + 1][m + 1];
    for (int i = 0; i <= n; ++i) sum[i][0] = 0;
    for (int i = 0; i <= m; ++i) sum[0][i] = 0; for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
    sum[i][j] = matrix[i - 1][j - 1] +
    sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; int max_value = Integer.MIN_VALUE;
    for (int i = k; i <= n; ++i)
    for (int j = k; j <= m; ++j) {
    int value = sum[i][j] - sum[i - k][j] -
    sum[i][j - k] + sum[i - k][j - k]; if (value > max_value)
    max_value = value;
    }
    return max_value;
    }
    }

      

Sliding Window Matrix Maximum的更多相关文章

  1. LintCode Sliding Window Matrix Maximum

    原题链接在这里:http://www.lintcode.com/zh-cn/problem/sliding-window-matrix-maximum/ 题目: Given an array of n ...

  2. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  3. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  4. Sliding Window Maximum 解答

    Question Given an array of n integer with duplicate number, and a moving window(size k), move the wi ...

  5. Sliding Window Maximum

    (http://leetcode.com/2011/01/sliding-window-maximum.html) A long array A[] is given to you. There is ...

  6. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  7. Sliding Window Maximum LT239

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  8. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  9. LeetCode题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

随机推荐

  1. Jenkins+gitlab+msbuild

    配置gitlab 这里会生成一个token在页面上方,一定要复制出来.存在别的地方. jenkins配置gitlab 在jenkins服务器上安装vs,目的是使用它的msbuild,如果项目中还用到了 ...

  2. RestTemplate相关组件:ClientHttpRequestInterceptor【享学Spring MVC】

    每篇一句 做事的人和做梦的人最大的区别就是行动力 前言 本文为深入了解Spring提供的Rest调用客户端RestTemplate开山,对它相关的一些组件做讲解. Tips:请注意区分RestTemp ...

  3. Python之路【第二十一篇】:JS基础

    JavaScript的基础学习(一) 一.JavaScript概述 1.1 JavaScript的历史 ● 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在 ...

  4. C语言函数返回指针方法

    1.将函数内部定义的变量用static修饰 由于static修饰的变量,分配在静态内存区(类似于全局变量区),函数返回时,并不会释放内存,因此可以将要返回的变量加static修饰. int *test ...

  5. Java爬虫https网页内容报错SSLHandshakeException信任(忽略)所有SSL证书

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building f ...

  6. Java abstract 理解和学习

    /** * <html> * <body> * <P> Copyright JasonInternational Since 1994 https://github ...

  7. 遍历js中的数组

    可以使用js中的for循环,或者forEach方法:也可以使用Ext中的方法遍历js中的数组 代码如下: /** * 遍历数组 */ var arr = ['越南', '新加坡', '美国', '俄罗 ...

  8. Oracle数据库之查询

    一.单表简单查询: 1. select * from scott.emp 2.去重: --去除重复记录 select distinct ssex from java0322; select disti ...

  9. vue+element 按钮来回切换

    需求很简单,实现很容易,日常记录一下 templace代码: data数据声明: me'thods方法:

  10. springboot feign too many bytes written executing POST

    解決办法: pom添加: <dependency><groupId>io.github.openfeign</groupId><artifactId>f ...