题目描述:

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的更多相关文章

  1. 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)

    Leetcode363 思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数. 先求出 ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Codeforces Gym 100637B B. Lunch 找规律

    B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Des ...

  2. 使用C# 生成word记录

    private void button1_Click(object sender, System.EventArgs e) { object oMissing = System.Reflection. ...

  3. Meteor 加入账户系统

    Meteor 加入账户系统 我们给meteor加入一个账户系统 导入包 meteor add ian:accounts-ui-bootstrap-3 meteor add accounts-passw ...

  4. Web之真假分页

    在web设计中一个无法避免的问题就是分页显示.当数据量特别大的时候,我们不可能将全部的数据都在一个页面进行显示,假设这样将严重影响到它的美观性.所以在这个时候,分页显示则成为了我们的大功臣.当然分页也 ...

  5. 利用nf_conntrack机制存储路由,省去每包路由查找

    IP是无连接的,因此IP路由是每包一路由的,数据包通过查找路由表获取路由,这是现代操作协议协议栈IP路由的默认处理方式.可是假设协议栈具有流识别能力,是不是能够基于流来路由呢?答案无疑是肯定的. 设计 ...

  6. Asp.net生成随机不重复的函数(方法)

    // 生成三位毫秒字串         public static string Get_mSec()         {             string mSec = System.DateT ...

  7. 一元线性回归模型与最小二乘法及其C++实现

    原文:http://blog.csdn.net/qll125596718/article/details/8248249 监督学习中,如果预测的变量是离散的,我们称其为分类(如决策树,支持向量机等), ...

  8. C#_deepCopy

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...

  9. [Qt5] 减少dll依赖和大小(特别是webkit的大小和依赖)

    Qt5的依赖太多, 而且很dll非常大. 折腾了好久, 摸索了一些精简的方法. webkit是个非常蛋疼的东西, 依赖超多, 又很庞大. 所以需不需要webkit是完全不同的. 如何编译Qt5可以参考 ...

  10. 奥运会订票系统c语言代写源码下载

    制作能够实现2008北京奥运会网上订票的系统,能够实现购票人员注册.购票.管理人员可以设置各个比赛场地的赛事安排及票数. 程序要求实现的功能如下: 购票者信息注册:购票者可以用昵称和身份证进行注册,若 ...