Sliding Window Matrix Maximum
Description
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的更多相关文章
- LintCode Sliding Window Matrix Maximum
原题链接在这里:http://www.lintcode.com/zh-cn/problem/sliding-window-matrix-maximum/ 题目: Given an array of n ...
- 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 ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
随机推荐
- 如何理解 PHP的依赖注入(DI) 和 控制反转(IoC)
名词解释: IoC - Inversion of Control 控制反转 DI - Dependency Injection 依赖注入 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式, ...
- composer安装FOSUserBundle内存溢出
内存溢出异常: Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 4096 bytes ...
- 如何修改通过Anaconda安装的jupyter notebook的工作目录
通过Anaconda安装jupyter notebook,对新手来说是一个非常明智的选择,可以避免很多不必要的麻烦! jupyter notbook默认情况下的工作目录是c:\user\...,接下来 ...
- golang --Converting and Checking Types
package main import ( "fmt" "strconv" ) func main() { strVar := "100" ...
- Docker 使用入门 Hello World
结合上一篇的所说的内容:现在安装好了Docker 安装好了就要用嘛,所以,先走一个Hello World docker run ubuntu:15.10 /bin/echo "hello w ...
- Java中守护线程的总结
在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用个比较通俗的比如,任何一个守护线程都是整个JVM中所有非守护线程的保姆: 只要当前JVM实例中尚存 ...
- C#泛型集合之——列表
列表基础 1.列表概述:列表与哈希集合不同之处在于,它的元素可以重复.(更接近逻辑上的数组,而哈希集合更接近于数学上的集合) 2.创建及初始化: (1)List<类型> 列表名 =new ...
- C# vb .net实现马赛克焦距像素化特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的马赛克焦距像素化效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置 ...
- [golang]使用gomail发邮件(在Go中发送电子邮件的最佳方式)
1 前言 定义邮箱服务器连接信息,如果是网易邮箱 pass填密码,qq邮箱填授权码(客户端专用密码). gomail包: go get gopkg.in/gomail.v2 更多功能可以参考 http ...
- 换个语言学一下 Golang (7)——使用函数
什么是函数 函数,简单来讲就是一段将输入数据转换为输出数据的公用代码块.当然有的时候函数的返回值为空,那么就是说输出数据为空.而真正的处理过程在函数内部已经完成了. 想一想我们为什么需要函数,最直接的 ...