【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 ...
随机推荐
- 《C语言编写 学生成绩管理系统》
/* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名: 学生成绩管理 ...
- .Net枚举类型小结
1.枚举类型的要点: (1)类型声明语法: enum 枚举名 (2)枚举体语法: a.成员名称 = 整数值,其他成员名称,或者其他成员与整数的表达式 b.成员之间需要用逗号隔开 (3)枚举可以继承的 ...
- linux对外开放某个端口命令
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /etc/rc.d/init.d/iptables save /etc/rc.d/init.d/ ...
- iOS开发——语法篇&swift经典语法总结
swift经典语法总结 1:函数 1.1 func funcNmae()->(){} 这样就定义了一个函数,它的参数为空,返回值为空,如果有参数和返回值直接写在两个括号里就可以了 1.2 参数需 ...
- 日志分析(三) kibana数据展示
kibana最新版本支持自定义数据大盘了,今天试用了下,感觉非常棒.上图: 支持多维度定义视图,然后视图里面可以依据粗粒度维度进行图表归类.总之,比旧版本强大太多了.后面试试能否扩展加入耦合一定业务数 ...
- [Effective C++ --032]确定你的public继承塑模出is-a
这一章都在讲述继承的关系.可以举个例子说明: 父类是水果,子类是苹果,苹果是一种(is-a)水果,但是水果不一定就是苹果. is-a并不是唯一存在classes之间的关系.另两个常见的关系是has-a ...
- string2array($value);
/*** 将字符串转换为数组** @param string $data 字符串* @return array 返回数组格式,如果,data为空,则返回空数组*/if ( !function_exis ...
- 琐碎-同步centos集群的时间
想马上上手HBase,其对集群时间同步要求很高,当然,hadoop也是
- Face The Right Way
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2564 Accepted: 117 ...
- Android 高级UI设计笔记06:仿微信图片选择器(转载)
仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...