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. Python之虚拟环境virtualenv、pipreqs生成项目依赖第三方包

    virtualenv简介 含义: virtual:虚拟,env:environment环境的简写,所以virtualenv就是虚拟环境,顾名思义,就是虚拟出来的一个新环境,比如我们使用的虚拟机.doc ...

  2. Linux 7 重置root密码

    在运维工作中经常会遇到不知道密码,密码遗忘,密码被他人修改过的情况,使用这种方式扫清你一切烦恼! 1.启动Linux系统,在出现引导界面时,按“e”键,进入内核编辑界面:2.找到有“linux16”的 ...

  3. 未能加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0

    写在前面 整理错误集.某一天在启动项目的时候,出现了未能加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0错 ...

  4. Nikitosh 和异或(trie树)

    题目: #10051. 「一本通 2.3 例 3」Nikitosh 和异或 解析: 首先我们知道一个性质\(x\oplus x=0\) 我们要求\[\bigoplus_{i = l}^ra_i\]的话 ...

  5. rename file

    https://askubuntu.com/questions/790633/the-o-parameter-in-aria2c-cant-rename-the-downloaded-file You ...

  6. Java 之 Jedis

    一.客户端 Jedis 1.Jedis Jedis 是一款java操作 redis 数据库的工具. 2.使用步骤 (1)下载 Jedis 的 jar 包 (2)使用: //1. 获取连接 Jedis ...

  7. mysql DCL数据控制语言

    -- 维护性操作   都是在cmd下操作的连接数据库:        本机:mysql [-h localhost] -u account -p        远程:mysql [-h remote_ ...

  8. hadoop2.8 ha 集群搭建

    简介: 最近在看hadoop的一些知识,下面搭建一个ha (高可用)的hadoop完整分布式集群: hadoop的单机,伪分布式,分布式安装 hadoop2.8 集群 1 (伪分布式搭建 hadoop ...

  9. 有趣for循环

    String fileValue = "2;3;4;5;6;"; String[] arry = fileValue.split(";"); for (int ...

  10. node基础学习——http基础知识-01-客户单请求

    <一> HTTP基础createServer()相关事件介绍 1. 创建HTTP服务器 server = http.createServer([requestListener]) // 下 ...