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 ...
随机推荐
- [转帖]美团在Redis上踩过的一些坑-1.客户端周期性出现connect timeout
美团在Redis上踩过的一些坑-1.客户端周期性出现connect timeout 博客分类: redis 运维 jedisconnect timeoutnosqltcp 转载请注明出处哈:http ...
- Mysql 语句 insert into 与 replace into 区别
[]insert into 与 replace into 区别 replace into 的运行与insert into 很相似.不同点: 若表中的一个旧记录与一个用于PRIMARY KEY 或 一个 ...
- 抽象工厂(AbstractFactory)模式
抽象工厂模式又称工具箱模式.其实抽象工厂模式可以简单的理解为一个工厂生成一个产品族的产品. 抽象工厂模式可以向客户端提供一个接口,使得客户端在不指定产品的具体类型的情况下,创建多个产品族中的产品对象. ...
- 安装rabbitMQ的PHP扩展
1.环境准备:centos 7.6+PHP7.3 2.安装rabbitmq-ctar xf rabbitmq-c-0.9.0.tar.gzcd rabbitmq-c-0.9.0mkdir build ...
- react-router的BrowserHistory 和 HashHistory 的区别,如何解决使用BrowserHistory 引起的访问路径问题
一,使用createBrowserHistory 和 createHashHistory 的 区别体现 1. 使用createBrowserHistory () // 使用createBrowserH ...
- java之spring之spring整合hibernate
这篇讲下spring和hibernate的整合 目录结构如下: 1.新建java项目 2.导入jar包 antlr-2.7.7.jar aopalliance.jar aspectjweaver.ja ...
- json_rpc_2 implementation
https://stackoverflow.com/questions/52670255/flutter-json-rpc-2-implementation import 'dart:convert' ...
- 两个数组的交集 II
题纲 给定两个数组,编写一个函数来计算它们的交集. 示例 : 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [4,9] 说明: 输出结果中每个元素出现的次数 ...
- 解决java依赖poi导出Excel表时,没有出现下载提示的问题
转自:https://blog.csdn.net/jinchunzhao123/article/details/88626077 浏览器响应: 而且进入断点调试,所有的数据都执行了就是没有下载提示.而 ...
- python通过装饰器检查函数参数的数据类型的代码
把内容过程中比较常用的一些内容记录起来,下面内容段是关于python通过装饰器检查函数参数的数据类型的内容. def check_accepts(f): assert len(types) == f. ...