当前位置:网站首页>力扣练习—— 矩形区域不超过 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;
}
边栏推荐
- 为什么Redis很快
- mysql5.7安装部署-yum安装
- 短视频软件开发——平台同质化如何破局
- Get started quickly and conquer three different distributed architecture calling schemes
- What is an abstract class
- GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
- Some tips for using Unsafe
- JWT implements login authentication + Token automatic renewal scheme
- POJ 1026 Cipher (置换群)
- 做自媒体月入几万?博主们都在用的几个自媒体工具
猜你喜欢

Some tips for using Unsafe

STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create

The impact of development mode on testing

自媒体爆款标题怎么写?手把手教你写热门标题

runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function

从源码角度分析UUID的实现原理

mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded

4 of huawei offer levels, incredibly side is easing the bit in the interview ali?

蔚来-软件开发工程师一面记录

推荐6个自媒体领域,轻松易上手
随机推荐
企业如何判断数据治理是否成功?
Double.doubleToLongBits()方法使用
Three-phase 380V rectified voltage
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
程序员追求技术夯实基础学习路线建议
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
Mobile and PC compatible loading and toast message plugins
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
1-IMU参数解析以及选择
ISO9001在讲什么?过程方法和风险思维
振弦传感器及核心VM系列振弦采集模块
第2章-矩阵及其运算-矩阵创建(1)
blocking non-blocking poll mechanism asynchronous
Codeforces 814 C. An impassioned circulation of affection (dp)
Store limited time seckill function system
如何使用工程仪器设备在线监测管理系统
Nocalhost - 让云原生时代的开发更高效
2022年裁员潮,失业程序员何去何从?
第2章-矩阵及其运算-矩阵运算(2)
学长告诉我,大厂MySQL都是通过SSH连接的