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. [转帖]POW , POS 与 DPOS 一切都为了共识

    POW , POS 与 DPOS 一切都为了共识 https://www.jianshu.com/p/f99e8fe57c9a   共识机制的背景 加密货币都是去中心化的,去中心化的基础就是P2P节点 ...

  2. sublime text 疑难解决

    sublime text 白色边框方框解决方法 https://blog.csdn.net/weixin_43228019/article/details/82766316 Sublime Text提 ...

  3. pytest_skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试. 常见示例是 ...

  4. JAVA8的java.util.function包

    一 概述 name type description Consumer Consumer< T > 接收T对象,不返回值 Predicate Predicate< T > 接收 ...

  5. java之mybatis之缓存

    1.mybatis自带缓存功能.分为一级缓存,二级缓存. 2.一级缓存为 session 缓存,在一个 session中 ,一个查询的 select 语句只会执行一次,根据  <select&g ...

  6. C# vb .net实现移除像素特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的移除像素特效呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  7. Windows 7 下安装 docker

    Windows 7 下需要安装docker toolbox即可(里面打包了docker.oracle virtualbox.Git) 1. 下载 1. 下载路径https://github.com/d ...

  8. python 3.url了解与基础使用

    URL使用 视图: 我们运行项目在网页上查看到的我们称之为视图 视图一般在views.py下编辑 它的第一个参数永远都是request,通过它请求一些数据返回给网页给我们查看. 视图函数的返回结果必须 ...

  9. Error creating bean with name 'XXX' defined in file

    这个错误是我在之前操作时,错将另一个dubbo服务器也加载到了该dubbo服务器上(pom.xml),所以出现了Error creating bean with name 'XXX' defined ...

  10. Java自学-数组 创建数组

    Java 如何创建一个数组 数组是一个固定长度的,包含了相同类型数据的 容器 步骤 1 : 声明数组 int[] a; 声明了一个数组变量. []表示该变量是一个数组 int 表示数组里的每一个元素都 ...