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 ...
随机推荐
- day49——圆形头像、定位、z-index、js
day49 今日内容 圆形头像 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- 怎么删除json 键值对
var obj = { 'id': 1, 'name': 2 } delete obj.id delete obj['id'] console.log(obj) //{ 'name': 2 }
- nacos初探--作为配置中心
什么是nacos Nacos 支持基于 DNS 和基于 RPC 的服务发现(可以作为springcloud的注册中心).动态配置服务(可以做配置中心).动态 DNS 服务. 官方介绍是这样的: Nac ...
- liunx下Oracle安装
1. 引言 将近一个月没有更新博客了,最近忙着数据库数据迁移工作:自己在服务器上搭建了oracle数据库,一步步走下来遇见很多BUG:现在自己记录下,方便以后有用上的地方: 2. 准备工作 oracl ...
- Linux生产环境上,最常用的一套“AWK“技巧【转】
最有用系列: <Linux生产环境上,最常用的一套“vim“技巧> <Linux生产环境上,最常用的一套“Sed“技巧> <Linux生产环境上,最常用的一套“AWK“技 ...
- 使用jmeter对dubbo接口进行性能测试教程及常见问题处理
一. 测试脚本编写 脚本可参考git项目: https://github.com/aland-1415/dubbo-interface-test.git 1. pom依赖 (注意添加的jmeter ...
- 【JVM】jdk1.8-jetty-swap被占满问题排查
背景 线上服务收到报警,报警内容:虚拟机swap区占用比例超过80%,如图: 本文着重描述排查问题的过程,在这个过程中不断的猜测–>验证–>推翻–>再猜测–>再验证–>再 ...
- js模块基础练习题
题目描述 完成函数 createModule,调用之后满足如下要求: 1.返回一个对象 2.对象的 greeting 属性值等于 str1, name 属性值等于 str2 3.对象存在一个 sayI ...
- Flask--配置文件
配置文件 配置文件的方式有很多,下面介绍两种: 第一种:根据全局变量实现 App.py from flask import Flask, session app = Flask(__name__) # ...
- Set,List,Map,Collection
// JAVA集合主要分为三种类型: // // Set(集) List(列表) Map(映射) Collection 接口 // // Collection是最基本的集合接口,声明了适用于JAVA集 ...