363. Max Sum of Rectangle No Larger Than K
来源:互联网 发布:lol韩服mac 下载地址 编辑:程序博客网 时间:2024/06/05 21:06
363. Max Sum of Rectangle No Larger Than K
最大矩形和不超过k
题目:
给定一个矩阵matrix和一个整数k,求元素加和不超过k的最大子矩形。
解题思路:
动态规划。
我们使用暴力搜索。
定义sum[i][j]为矩形(0,0)-(i,j)的和;然后我们遍历这个矩形中的所有的子矩形,计算它与k的大小,并更新结果。
代码:
class Solution {public int maxSumSubmatrix(int[][]matrix, int k){if(matrix==null || matrix.length==0 ) return 0;int m=matrix.length;int n=matrix[0].length;int sum[][]=new int[m][n];sum[0][0]=matrix[0][0];int result=Integer.MIN_VALUE;for(int i=0;i<m;i++){for(int j=0;j<n;j++){int t=matrix[i][j];if(i>0) t+=sum[i-1][j];if(j>0) t+=sum[i][j-1];if(i>0 && j>0) t-=sum[i-1][j-1];sum[i][j]=t;for(int l=0;l<=i;l++){for(int r=0;r<=j;r++){int d=sum[i][j];if(l>0) d-=sum[l-1][j];if(r>0) d-=sum[i][r-1];if(l>0 && r>0) d+=sum[l-1][r-1];if(d<=k) result=Math.max(result, d);}}}}return result;}}
阅读全文
0 0
- [leetcode] 363. Max Sum of Rectangle No Larger Than K
- leetcode.363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- [LeetCode]--363. Max Sum of Rectangle No Larger Than K
- leetcode-363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K[hard]
- 363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- Leetcode 363. Max Sum of Rectangle No Larger Than K
- 【Leetcode】363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- [leetcode]363. Max Sum of Rectangle No Larger Than K
- leetcode 363. Max Sum of Rectangle No Larger Than K
- 363. Max Sum of Rectangle No Larger Than K
- Leetcode Max Sum of Rectangle No Larger Than K
- Max Sum of Rectangle No Larger Than K
- [LeetCode]Max Sum of Rectangle No Larger Than K
- julius开源语音识别引擎
- 354. Russian Doll Envelopes
- HDOJ 1094 A+B for Input-Output Practice (VI)
- 【模板】LCT
- HDOJ 1095 A+B for Input-Output Practice (VII)
- 363. Max Sum of Rectangle No Larger Than K
- Vim操作的四种模式
- 关于Python中的参数定义及可变参数
- 面向对象程序设计上机练习九(对象指针)
- java魔法币问题
- 898ARounding
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现
- 375. Guess Number Higher or Lower II
- tqdm==>python可扩展进度条