原题链接在这里: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. range基础

    collapse这个方法是把结束位置抛弃掉,并不是简单的设置到开始位置. 结束位置被抛弃掉以后,只要没有给它重新设置位置,它就一直都会等 于开始位置.即使你修改了开始位置,结束位置还是会在修改后的开始 ...

  2. [转] 我所经历的IBM SOA与系统整合

    2006年,一汽大众新上来一位总经理,新加坡人,整个一国际背景高端管理人才,是德国人和中国人博弈后取得的胜利. 一汽大众过去是从总部到区域到终端一体化的大盘管理模式,很僵化很粘稠,不利用快速反应.于是 ...

  3. RocketMQ 笔记-转

    Astrotrain概述 Astrotrain是基于阿里巴巴开源项目RocketMQ进行封装的分布式消息中间件系统,提供集群环境下的消息生产和消费功能. RocketMQ介绍 RocketMQ的物理部 ...

  4. 容器排序之sort,stable_sort

    bool isShorter(const string &s1, const string &sz){ return s1.size() < sz.size(); } int m ...

  5. 【CodeChef】Turbo Sort

    题目链接:Turbo Sort 用java自带O(NlogN)的排序就可以,java要特别注意输入输出.输入用BufferedReader,输出用printWriter.printWriter的速度比 ...

  6. Linux的压缩命令(tar,gzip,zip)

    打包和压缩.打包是指将一大堆文件或目录变成一个总的文件:压缩则是将一个大的文件通过一些压缩算法变成一个小文件. 这源于Linux中很多压缩程序只能针对一个文件进行压缩,这样当你想要压缩一大堆文件时,你 ...

  7. Docker 容器监控平台-Weave Scope

    官网地址:https://www.weave.works/oss/scope/ 安装 执行如下脚本安装运行 Weave Scope. curl -L git.io/scope -o /usr/loca ...

  8. eclipse设置高亮显示的颜色

    设置高亮显示的颜色:Window-->preferences-->General-->Editors-->Text Editors-->Annotations--> ...

  9. centos7安装MPlyaer

    最近更换了centos7系统,对新系统的操作不是太熟悉.大神轻喷.昨晚突然想要下个电影看看,结果发现系统自带的播放器支持的视频格式有限,google查了一下,他们推荐使用MPlayer.于是经过一通g ...

  10. BZOJ3243/UOJ121 [Noi2013]向量内积

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...