LintCode Sliding Window Matrix Maximum
原题链接在这里: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的更多相关文章
- Sliding Window Matrix Maximum
Description Given an array of n * m matrix, and a moving matrix window (size k * k), move the window ...
- LintCode "Sliding Window Median" & "Data Stream Median"
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
- 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 ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- Sliding Window Maximum 解答
Question Given an array of n integer with duplicate number, and a moving window(size k), move the wi ...
- Sliding Window Maximum
(http://leetcode.com/2011/01/sliding-window-maximum.html) A long array A[] is given to you. There is ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 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 ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
随机推荐
- dockfile
dockerfile是对镜像的描述 新建一个dockfile文件 docker inspect
- php大转盘抽奖
抽奖大转盘演示:http://www.sucaihuo.com/php/3301.html function getRand($proArr, $proCount) { $result = ''; $ ...
- 使用git从本地上传至git码云远程仓库
从 http://git-scm.com/download 下载window版的客户端.下载好,一步一步安装即可. 使用前的基本设置 git config --global user.name & ...
- HTML学习笔记(上)
1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...
- iptable防火墙面试题
第1章 (一)基础口试题 1.1 详述 iptales 工作流程以及规则过滤顺序? 1.防火墙是一层层过滤的.实际是按照配置规则的顺序从上到下,从前到后进行过滤的. 2.如果匹配上了规则,即明确表明是 ...
- Myeclipse中Tomcat的两种部署方式
一.在Myeclipse软件中部署 1. 在Myeclipse中,创建好工程后,在Myeclipse菜单栏中选择 Windows -> Preferences -> Myeclipse - ...
- springmvc注解基本入门
简单介绍使用springmvc注解的基本流程. 1.在web.xml中配置DispatcherServlet <?xml version="1.0" encoding=&qu ...
- linux下java unrecognized class file version错误的解决
root@Mr javaPC]# java HelloWorldException in thread “main” java.lang.ClassFormatError: HelloWorld (u ...
- 使用 Apache Spark 让 MySQL 查询速度提升 10 倍以上
转: https://coyee.com/article/11012-how-apache-spark-makes-your-slow-mysql-queries-10x-faster-or-more ...
- 【P1379】八数码难题(搜索+暴力)
这个题真是... 不想说什么了,及其复杂和烦人的一道题.基础思路就是bfs,用两个队列分别进行0的位置的计算和每一步的状态..然而这个题最重要的一点在于判重,实际上可以康托展开用全排列的个数进行判重, ...