【leetcode】363. Max Sum of Rectangle No Larger Than K
题目描述:
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.
解题思路:
根据题意,寻找二维数组中所有可以组成的矩形中面积不超过k的最大值,所以必须要求出可能组成的矩形的面积并与k比较求出最终结果。这里为了最终不超时,可以在一下方面进行优化:
1.设置一个数组比较当前列(或者行)下已经扫描过的数的和。
2.设置一个TreeMap,保存当前矩阵长度下,已经扫描过得矩形的面积。同时,TreeMap中有快速查找元素的方法,可以快速查找所找的元素。
具体代码:
public class Solution {
public static int maxSumSubmatrix(int[][] matrix, int target) {
int row = matrix.length;
if(row==0)
return 0;
int col = matrix[0].length;
if(col==0)
return 0;
int result = Integer.MIN_VALUE;
boolean key= col>row?false:true;
int m = Math.min(row,col);
int n = Math.max(row,col);
//一行一行的找
for(int i=0;i<m;i++){
//找从第i行开始一直到第0行这i+1行的可能组成的矩形长度
int[] array = new int[n];//这个矩阵为了保存每一列上第j行到第i行的和
for(int j=i;j>=0;j--){
TreeSet<Integer> set = new TreeSet<Integer>();//用来保存当前高度下,长度为从0开始到k位置的矩形的结果。理解set的含义是解决此题的关键。
set.add(0);
int sum=0;
for(int k=0;k<n;k++){
if(key){
array[k]+=matrix[k][j];
}
else{
array[k]+=matrix[j][k];
}
sum+=array[k];
/* 因为要满足 (sum-set中的元素)<=target,
* 而且sum-set中的元素的值要尽可能的大,
* 所以也就是再求小于等于sum-target中满足条件的元素的最小的一个
* 正好TreeSet中提供了这个方法ceil(),可以很方便的找出这个元素
*/
Integer integer = set.ceiling(sum-target);
if(integer!=null){
result=Math.max(result, sum-integer);
}
set.add(sum);
}
}
}
return result;
}
}
【leetcode】363. Max Sum of Rectangle No Larger Than K的更多相关文章
- 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
- 363. Max Sum of Rectangle No Larger Than K
/* * 363. Max Sum of Rectangle No Larger Than K * 2016-7-15 by Mingyang */ public int maxSumSubmatri ...
- [LeetCode] 363. Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- 363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- 第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)
Leetcode363 思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数. 先求出 ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- Leetcode: Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [Swift]LeetCode363. 矩形区域不超过 K 的最大数值和 | Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
随机推荐
- Swift学习笔记一
最近计划把Swift语言系统学习一下,然后将MagViewer用这种新语言重构一次,并且优化一下,这里记录一下Swift的学习笔记. Swift和Objective-C相比,在语法和书写形式上做了很多 ...
- PS常见错误-无法完成请求,因为文件格式模块不能解析该文件
无法完成请求,因为文件格式模块不能解析该文件 将图片格式变成.jpg格式就可以了
- Android通过http协议POST传输方式
Android通过http协议POST传输方式如下: 方式一:HttpPost(import org.apache.http.client.methods.HttpPost) 代码如下: privat ...
- Android Mms 接收信息流程
信息的接收工作是由底层来完成的,当有一个 新的信息时底层完成接收后会以Intent的方式来通知上层应用,信息的相关内容也包含在Intent当中,Android所支持的信息Intent都定 义在andr ...
- memcpy的用法总结
1.memcpy 函数用于 把资源内存(src所指向的内存区域) 拷贝到目标内存(dest所指向的内存区域):拷贝多少个?有一个size变量控制拷贝的字节数:函数原型:void *memcpy(voi ...
- ConfigurationManager.GetSection()方法的使用
GetSection方法读取的是configSections节点,这个节点在web.config配置文件中,它比较特殊,必须放置于首节点,也就是说,在它之前不能有其它类型的节点.configSecti ...
- 进程间通信之popen和pclose函数
常见的操作是创建一个管道连接到另一个进程,然后读其输出或向其输入端发送数据,为此,标准I/O库提供了两个函数popen和pclose.这两个函数实现的操作是:创建一个管道,调用fork产生一个子进程, ...
- 配置phpmyadmin使登录时可填写IP管理多台MySQL 连接多个数据库 自动登录
一.如何设置phpMyAdmin自动登录? 首先在根目录找到config.sample.inc.php复制一份文件名改为config.inc.php(如果已经存在 config.inc.php 文件, ...
- Vesions ignore & ld: library not found for -l...
1.递归删除指定目录下的 .git..svn 文件 find . -name .git | xargs rm -fr find . -name .svn | xargs rm -rf 第一条倒还不常用 ...
- linux read 用法
1.基本读取 read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数据放入一个标准变量中.下面是 read命令 的最简单形式:: #!/bin/bas ...