原题链接在这里: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. text_field text_tag 用法

    = f.text_field :tax_category_id, :value => @invoice.tax_category.name, :class => "form-co ...

  2. sql两张表关联更新字段

    记录,推荐使用第三种,个人觉得比较好 --第一种写法,之前一直用,没有在意 UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFl ...

  3. Java中ArrayList和LinkedList区别、ArrayList和Vector的区别

    一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,Ar ...

  4. android 加固防止反编译-重新打包

    http://blog.csdn.net/u010921385/article/details/52505094 1.需要加密的Apk(源Apk) 2.壳程序Apk(负责解密Apk工作) 3.加密工具 ...

  5. perl FAQ(zz)

    1. Why do you write a program in Perl? Ans : Easy to use and fast execution since perl script underg ...

  6. MySQL-重做日志 redo log -原理

    [redo log buffer][redo log file]-原理 目录: 1.重做日志写入过程图 2.相关知识点汇总图 3.redo_log_buffer 原理 4.redo_log_file ...

  7. jack server 常见错误解决方法【转】

    本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...

  8. 基于matlab的边缘提取方法的比较

    1.Matlab简述 Matlab是国际上最流行的科学与工程计算的软件工具,它起源于矩阵运算,已经发展成一种高度集成的计算机语言.有人称它为“第四代”计算机语言,它提供了强大的科学运算.灵活的程序设计 ...

  9. RazorEngine性能研究(反射的延深)

    先说下结论 1)RazorEngine 确实很慢,编译过程特别慢,编译过后仍不适合大量重复调用的情况(一次调用可以接受). 2 )   RazorEngine 和 asp.net mvc 里的Razo ...

  10. Mybatis-config.xml配置文件详解

    1.官方给出的案列: 注意:这些配置在文件中的顺序非常重要!必须严格按照上图中出现的顺序定义 2.properties属性 该属性主要作用就是引入外部的properties是文件,文件格式为xxx=x ...