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单机多节点集群
# ##安装Redis redis安装参考 https://www.cnblogs.com/renxixao/p/11442770.html Reids安装包里有个集群工具,要复制到/usr/loca ...
- 2019-7-17 正则表达式和re模块
一.re模块与正则表达式之间的关系 正则表达式不是python独有的,它是一门独立的技术 所有的编程语言都可以使用正则 但是如果你想在python中使用,你就必须依赖于re模块 正则的官方定义:正则表 ...
- 【基本知识】UART接口
1.简介 (1)UART一种通用异步串口数据总线,最低采用两路信号(TX/RX)即可实现全双工通信,十分简单: (2)UART采用LSB模式传输,串口数据传输格式如下图所示: 起始位:长度为1位的时间 ...
- Linux磁盘设备基础
free -m 查看系统内存 [root@zhang /]# free -m total used free shared buffers cached ...
- lsyncd实时同步工具
简介 Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实 ...
- vue SPA设计 history hash
<body> <h3>Histort api</h3> <a class="api a">a,html</a> < ...
- ELK学习笔记之使用curl命令操作elasticsearch
0x00 _cat系列 _cat系列提供了一系列查询elasticsearch集群状态的接口.你可以通过执行curl -XGET localhost:9200/_cat 1. 获取所有_cat系列的操 ...
- MySQL单表数据量过千万,采坑优化记录,完美解决方案
问题概述 使用阿里云rds for MySQL数据库(就是MySQL5.6版本),有个用户上网记录表6个月的数据量近2000万,保留最近一年的数据量达到4000万,查询速度极慢,日常卡死.严重影响业务 ...
- Eclipse创建ssm项目
1.创建Maven项目 2.勾选上面的 3.打成war包的形式 4.配置webapp.xml Project Facets——Dynamic Wed Module 2.5 ——然后点击下面的提示 5 ...
- 07、MySQL—时间日期类型
时间日期类型 1.Date 日期类型:系统使用三个字节来存储数据,对应的格式为:YYYY-mm-dd,能表示的范围是从1000-01-01 到9999-12-12,初始值为0000-00-00 2.T ...