当前位置:网站首页>力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
2022-08-10 11:00:00 【qq_43403657】
矩形区域不超过 K 的最大数值和
1.问题描述
给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和。
示例:
输入: matrix = [[1,0,1],[0,-2,3]], k = 2
输出: 2
解释: 矩形区域 [[0, 1], [-2, 3]] 的数值和是 2,且 2 是不超过 k 的最大数字(k = 2)。
说明:
矩阵内的矩形区域面积必须大于 0。
如果行数远大于列数,你将如何解答呢?
[OJ中有个样例就是1300行 13列 ,注意超时]
2.输入说明
首先输入matrix的行数m、列数n,
然后输入m行,每行n个整数。
最后输入一个整数k。
3.输出说明
输出一个整数。
4.范例
输入
2 3
1 0 1
0 -2 3
2
输出
2
5.代码
#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstring>
#include<algorithm>
#include<set>
using namespace std;
int findMaxSum(vector<vector<int> >matrix, int k)
{
//思想参考:https://leetcode.cn/problems/max-sum-of-rectangle-no-larger-than-k/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-dh8s/
if (matrix.empty() || matrix[0].empty()) return 0;//特殊情形判断
int m = matrix.size();//行
int n = matrix[0].size();//列
int res = INT_MIN;
int sum[m][n];//这里使用vector部分样例会超时
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;
}
int main()
{
vector<vector<int> > nums;
int m,n,k,tmp;
cin >> m >>n;
for (int i = 0; i < m; i++)
{
vector<int> t;
t.clear();
for (int j = 0; j < n; j++)
{
cin >> tmp;
t.push_back(tmp);
}
nums.push_back(t);
}
cin >> k;
int res = findMaxSum(nums, k);
cout << res << endl;
return 0;
}
边栏推荐
- 第2章-矩阵及其运算-矩阵创建(1)
- L2 applications from a product perspective: why is it a playground?
- 三相380V整流后的电压
- 这些年我开源的几个小项目
- 第5章相似矩阵及二次型(4)
- 内存问题难定位,那是因为你没用ASAN
- Codeforces 814 C. An impassioned circulation of affection (dp)
- Mobile and PC compatible loading and toast message plugins
- EasyCVR级联时,修改下级平台名称将不同步至上级平台
- How can an organization judge the success of data governance?
猜你喜欢
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

LAXCUS分布式操作系统安全管理

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

建校仅11年就入选“双一流” ,这所高校是凭什么做到的?

用proteus直接仿真stm32-可以完全丢弃编程器

Dry goods!ASSANet: Making PointNet++ faster and stronger

Research on motion capture system for indoor combined positioning technology

1-IMU参数解析以及选择

基于UiAutomator2+PageObject模式开展APP自动化测试实战

2023版揽胜运动曝光,安全、舒适一个不落
随机推荐
三相380V整流后的电压
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Short video software development - how to break the platform homogenization
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
不止跑路,拯救误操作rm -rf /*的小伙儿
中小规模网站架构
3 injured in 'electrical accident' at Google data center
HDU 1520 Anniversary party (tree dp)
【勇敢饭饭,不怕刷题之链表】有序链表的合并
阻塞 非阻塞 poll机制 异步
力扣练习——59 从二叉搜索树到更大和树
使用cpolar远程连接群晖NAS(升级固定链接2)
LeetCode_152_乘积最大子数组
越折腾越好用的 3 款开源 APP
2023版揽胜运动曝光,安全、舒适一个不落
怎么加入自媒体,了解这5种变现模式,让账号快速变现
Redis设计与实现
第5章相似矩阵及二次型(4)