当前位置:网站首页>Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
2022-08-10 11:33: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.
如果行数远大于列数,你将如何解答呢?
[OJThere is an example in 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;//Judgment of special circumstances
int m = matrix.size();//行
int n = matrix[0].size();//列
int res = INT_MIN;
int sum[m][n];//这里使用vectorSome samples will time out
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;
}
边栏推荐
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- Gartner reiterates the important value of 'data weaving'
- POJ 3101 Astronomy (Mathematics)
- 2023版揽胜运动曝光,安全、舒适一个不落
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- Introduction to Software Architecture
- 力扣练习——64 最长和谐子序列
- 如何使用工程仪器设备在线监测管理系统
- 【电商运营】你真的了解社交媒体营销(SMM)吗?
- Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
猜你喜欢

rider内Mono脚本找不到引用资源

模块九 - 设计电商秒杀系统

Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using

mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded

Module 9 - Designing an e-commerce seckill system

【勇敢饭饭,不怕刷题之链表】链表中有环的问题

Where can I view the version record of WeChat applet submission review history?

A case of violent parameter tuning in machine learning

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

From the product dimension, why can't we fully trust Layer2?
随机推荐
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
第2章-矩阵及其运算-矩阵创建(1)
什么是幂等性?四种接口幂等性方案详解!
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
第二十二章 源代码文件 REST API 参考(四)
Weilai-software development engineer side record
振弦传感器及核心VM系列振弦采集模块
从源码角度分析UUID的实现原理
[E-commerce operation] Do you really understand social media marketing (SMM)?
做自媒体月入几万?博主们都在用的几个自媒体工具
Nocalhost - 让云原生时代的开发更高效
ISO9001在讲什么?过程方法和风险思维
【机器学习】浅谈正规方程法&梯度下降
机器学习之暴力调参案例
HDU 1520 Anniversary party (树型dp)
【勇敢饭饭,不怕刷题之链表】有序链表的合并
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
The brave rice rice, does not fear the brush list of 】 list has a ring
自媒体爆款标题怎么写?手把手教你写热门标题