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 such that its sum is no larger than k.
Example:
Given matrix = [
[1, 0, 1],
[0, -2, 3]
]
k = 2
The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).
Note:
The rectangle inside the matrix must have an area > 0.
What if the number of rows is much larger than the number of columns?
详见:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/description/
C++:
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>>& matrix, int k)
{
if (matrix.empty() || matrix[0].empty())
{
return 0;
}
int m = matrix.size(), n = matrix[0].size(), res = INT_MIN;
int sum[m][n];
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 r = 0; r <= i; ++r)
{
for (int c = 0; c <= j; ++c)
{
int d = sum[i][j];
if (r > 0)
{
d -= sum[r - 1][j];
}
if (c > 0)
{
d -= sum[i][c - 1];
}
if (r > 0 && c > 0)
{
d += sum[r - 1][c - 1];
}
if (d <= k)
{
res = max(res, d);
}
}
}
}
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/5617660.html
363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K的更多相关文章
- 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 ...
- 【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 ma ...
- 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
- 第十三周 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 ...
- 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 ...
随机推荐
- .NET-高并发及限流方案
前言:高并发对我们来说应该都不陌生,特别想淘宝秒杀,竞价等等,使用的非常多,如何在高并发的情况下,使用限流,保证业务的进行呢.以下是一个实例,不喜勿喷! 总体思路: 1. 用一个环形来代表通过的请求 ...
- c#读取.config文件内容
今天在做项目的时候,由于程序同时启动多种情况的数据,测试分为多个人,就需要把数据分离开来,于是用了一个临时的配置文件,让测试在配置文件修改相应数据从而让各个测试互相不影响! 步骤: 第一步:添加一个A ...
- 【Codeforces 356A】Knight Tournament
[链接] 我是链接,点我呀:) [题意] n个人矩形m场比赛 每场比赛由编号为li~ri且之前没有被淘汰的人进行. 已知第i场的winner是xi winner会把这一场其他所有的人都淘汰. 问你n个 ...
- CodeForcesGym 100753A A Journey to Greece
A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeFor ...
- poj 1364 查分约束
#include<stdio.h> #include<iostream> #include<stack> #include<string.h> usin ...
- poj 2823单调队列模板题
#include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N 1100000 int getmin[N],getmax[N],num[N],n,k, ...
- nyoj_518_取球游戏_201404161738
取球游戏 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个 ...
- 在Electron中通过ffi模块实现JavaScript调用C++动态库
目前在网上能搜到的JS调C++动态库的实现有两种,一种是通过开发Node.js addon模块的方式实现调用,这种对于我们已有的代码实现比较复杂,需要大量的开发,因此不适用:另一种是通过FFI模块,F ...
- ubuntu各文件夹简介 [转载]
原文地址:ubuntu各文件夹简介作者:SuperZhy ubuntu各文件夹简介 /bin 二进制可执行命令/dev 设备文件(硬盘/光驱等)/etc 系统管理和配置文件/etc/rc.d 启动的配 ...
- excel 补全全部空格
首先全选列或者选中某列,按F5键.再按"定位条件„"button,选择空值,这样就把全部列的空格选中了,然后直接输入"你想要替换的值",再按Ctrl + 回车