Max Sum of Rectangle No Larger Than K
2016-06-27 11:30
441 查看
public class Solution { public int maxSumSubmatrix(int[][] matrix, int k) { if (matrix.length == 0 || matrix[0].length == 0) { return 0; } int result = Integer.MIN_VALUE; int row = matrix.length, col = matrix[0].length; int m = Math.min(row, col); int n = Math.max(row, col); boolean isCol = row > col ? true : false; for (int i = 0; i < m; i++) { int[] sum = new int ; for (int j = i; j >= 0; j--) { int val = 0; TreeSet<Integer> set = new TreeSet<>(); set.add(0); for (int l = 0; l < n; l++) { sum[l] += isCol ? matrix[l][j] : matrix[j][l]; val += sum[l]; Integer subresult = set.ceiling(val - k); if (subresult != null) { result = Math.max(result, val - subresult); } set.add(val); } } } return result; } }
1. Set does not have ceiling method. Directly use TreeSet
2. ceiling > value - k, so value - ceiling < k.
3. Put 0 into set first since 0 should be the exactly upper bound.
相关文章推荐
- 机器学习常见算法分类汇总
- Java中时分秒毫秒格式(xx:xx:xx.xxx)加一个txt读出的整数值 转换为时间格式 并存入txt
- osx 安装pillow时出现的问题
- Java 中 String 类的常用方法
- byte数组转为图片
- # linux shell常用快捷键
- 数据预处理系列:(十二)用截断奇异值分解降维
- 微服务扩展新途径:Messaging
- SQLite更新数据列,给数据加一个前(后)缀
- 遇到PDF需要转换修改?现在只要在线使用就可 4000 以了
- HDU 4587 TWO NODES 割点
- mac系统如何显示和隐藏文件
- 使用Flexible实现手淘H5页面的终端适配二
- 如何实现“秒杀”系统
- JSON 和 List之互相转换
- Android中 获取某控件X Y轴坐标值
- qss 美化groupbox
- 剖析Disruptor:为什么会这么快?(三)揭秘内存屏障
- Design Hit Counter
- 计算机网络基础知识笔记(一)