当前位置: 首页 > news >正文

acwing-3194 最大的矩形

acwing-3194 最大的矩形

这个题程序设计课上有讲过,

平民算法,时间复杂度在 O ( n 2 ) O(n^2) O(n2)

//
// Created by HUAWEI on 2024/10/28.
//
#include<iostream>using namespace std;const int Max_size = 1e4 + 20;int N;
int h[Max_size];int main() {cin >> N;for (int i = 0; i < N; i++)cin >> h[i];int res = 0;for (int i = 0; i < N; i++) {int l = i - 1;int r = i + 1;while (l >= 0 and h[l] >= h[i])l--;while (r <= N - 1 and h[r] >= h[i])r++;int temp = (r - l - 1) * h[i];if (temp > res)res = temp;}cout << res << endl;return 0;
}

单调栈解决,时间复杂度在 O ( n ) O(n) O(n)

//
// Created by HUAWEI on 2024/10/28.
//
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>using namespace std;int largestArea(vector<int> &h) {// 单调栈返回最大矩形面积stack<int> s; //单调非减栈h.insert(h.begin(), -1);h.push_back(0);int len = h.size();int res = 0;s.push(0);for (int i = 1; i < len; i++) {while (h[i] < h[s.top()]) {int temp = s.top();s.pop();res = max(res, (h[temp] * (i - s.top() - 1)));}s.push(i);}return res;
}int main() {int n;vector<int> h;cin >> n;for (int i = 0; i < n; i++) {int temp;cin >> temp;h.push_back(temp);}cout << largestArea(h);return 0;
}

参考博客

http://www.lryc.cn/news/519930.html

相关文章:

  • UnityDemo-TheBrave-制作笔记
  • 玩转 JMeter:Random Order Controller让测试“乱”出花样
  • VTK知识学习(33)-交互问题2
  • Centos9-SSH免密登录配置-修改22端口-关闭密码登录-提高安全性
  • SqlServer: An expression services limit has been reached异常处理
  • CentOS下安装Docker
  • WPF控件Grid的布局和C1FlexGrid的多选应用
  • Jenkins-持续集成、交付、构建、部署、测试
  • 高级第一次作业
  • Copula算法原理和R语言股市收益率相依性可视化分析
  • 反弹SHELL不回显带外正反向连接防火墙出入站文件下载
  • 后盾人JS--JS值类型使用
  • 1月11日
  • 【深度学习】Pytorch:加载自定义数据集
  • 最近在盘gitlab.0.先review了一下docker
  • OA项目登录
  • verilogHDL仿真详解
  • 基于http协议的天气爬虫
  • _STM32关于CPU超频的参考_HAL
  • C#,图论与图算法,任意一对节点之间最短距离的弗洛伊德·沃肖尔(Floyd Warshall)算法与源程序
  • AWS云计算概览(自用留存,整理中)
  • 1. npm 常用命令详解
  • js:根据后端返回数据的最大值进行计算然后设置这个最大值为百分之百,其他的值除这个最大值
  • 【Spring】@Size 无法拦截null的原因
  • 【Block总结】掩码窗口自注意力 (M-WSA)
  • 用 HTML5 Canvas 和 JavaScript 实现雪花飘落特效
  • 【cocos creator】【ts】事件派发系统
  • 《探索鸿蒙Next上开发人工智能游戏应用的技术难点》
  • CSS | CSS实现两栏布局(左边定宽 右边自适应,左右成比自适应)
  • acwing_3195_有趣的数