原题链接在这里:http://www.lintcode.com/zh-cn/problem/sliding-window-matrix-maximum/

题目:

Given an array of n * m matrix, and a moving matrix window (size k * k), move the window from top left to botton right at each iteration, find the maximum number inside the window at each moving.
Return 0 if the answer does not exist.

For matrix

[
[1, 5, 3],
[3, 2, 1],
[4, 1, 9],
]

The moving window size k = 2
return 13.

At first the window is at the start of the array 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.

题解:

用sum matrix来表示以[i,j]为右下角点时整个左上方的matrix的和. sum[i][j] = matrix[i-1][j-1] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1].

需要减掉sum[i-1][j-1]因为加重复了.

然后每次算以[i][j]为右下角, 边长为k的小matrix的和, sum[i][j] - sum[i-k][j] - sum[i][j-k] + sum[i-k][j-k].

需要加上sum[i-k][j-k]因为减重复了.

Time Complexity: O(m*n), m = matrix.length, n = matrix[0].length.

Space: O(m*n).

AC Java:

 public class Solution {
public int maxSlidingMatrix(int[][] matrix, int k) {
if(matrix == null || matrix.length < k || matrix[0].length < k){
return 0;
}
int m = matrix.length;
int n = matrix[0].length;
int [][] sum = new int[m+1][n+1];
for(int i = 1; i<=m; i++){
for(int j = 1; j<=n; j++){
sum[i][j] = matrix[i-1][j-1] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];
}
} int res = Integer.MIN_VALUE;
for(int i = k; i<=m; i++){
for(int j = k; j<=n; j++){
res = Math.max(res, sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k]);
}
}
return res;
}
}

LintCode Sliding Window Matrix Maximum的更多相关文章

  1. Sliding Window Matrix Maximum

    Description Given an array of n * m matrix, and a moving matrix window (size k * k), move the window ...

  2. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

  3. 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 ...

  4. leetcode面试准备:Sliding Window Maximum

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

  5. Sliding Window Maximum 解答

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

  6. Sliding Window Maximum

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

  7. 【LeetCode】239. Sliding Window Maximum

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

  8. 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 ...

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

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

随机推荐

  1. Shell 条件判断总结

    -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目录,则为真 -e file 若文件存在,则为真 -f ...

  2. 初识python---简介,简单的for,while&if

    一编程语言:编程语言是程序员与计算机沟通的介质: 编程语言的分类:  1机器语言:是用二进制代码表示的计算机能直接识别和执行的一种机器指令的集合.           优点:灵活,直接执行和速度快   ...

  3. IEnumerable的一些基本方法

    在说明用法之后,先要弄点数据. class Product { public int ID { get; set; } public string Name { get; set; } public ...

  4. Efficient Vector Representation for Documents through Corruption-by Minmin Chen阅读

    关键词: 词向量.文档向量.文档表示 地址:https://openreview.net/forum?id=B1Igu2ogg&noteId=B1Igu2ogg 首先,论文解决的是Word2V ...

  5. 【P2514】工厂选址(贪心)

    看到题了不首先应该看看数据范围确定一下算法么,这个题的数据范围大约可以支持到O(nmlogm),所以肯定不是搜索什么的,DP貌似至少也要n^2m,所以可以想一些其他的.对于题目的输入,我们发现这些输入 ...

  6. LeetCode——Construct Binary Tree from Preorder and Inorder Traversal

    Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...

  7. jqgrid的scroll参数的使用

    scroll参数会影响addJSONData(data)方法的使用 存在scroll参数,addJSONData方法会往表格中追加数据: 不存在scroll参数时,addJSONData方法会覆盖表格 ...

  8. 中文乱码问题(页面乱码,eclipse乱码,请求响应乱码)

    1.首先在开发工具eclipse中设置工作空间和文件编码格式,详情参见   http://www.cnblogs.com/lixiang1993/p/7345161.html 2.在eclipse的安 ...

  9. PAT1030. Travel Plan (30)

    #include <iostream> #include <limits> #include <vector> using namespace std; int n ...

  10. codevs 2800 送外卖 TSP问题

    2800 送外卖 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond         题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份 ...