Ai
1 Star 0 Fork 0

JJustRight/ACM-ICPC-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
histogram_area.cpp 969 Bytes
一键复制 编辑 原始数据 按行查看 历史
matthewsamuel95 提交于 2017-10-27 23:50 +08:00 . Update histogram_area.cpp
#include <iostream>
#include <stack>
using namespace std;
int getMaxArea(int hist[], int n) {
stack < int > s;
int max_area = 0;
int tp;
int area_with_top;
int i = 0;
while (i < n) {
if (s.empty() || hist[s.top()] <= hist[i])
s.push(i++);
else {
tp = s.top();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);
if (max_area < area_with_top)
max_area = area_with_top;
}
}
while (s.empty() == false) {
tp = s.top();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);
if (max_area < area_with_top)
max_area = area_with_top;
}
return max_area;
}
int main() {
int n;
cout << "Enter the number of bars:";
cin >> n;
cout << endl;
int hist[n];
cout << "enter the heights of bars" << endl;
for (int i = 0; i < n; i++) {
cin >> hist[i];
}
cout << endl << "Maximum area is " << getMaxArea(hist, n);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jjustright/ACM-ICPC-Algorithms.git
git@gitee.com:jjustright/ACM-ICPC-Algorithms.git
jjustright
ACM-ICPC-Algorithms
ACM-ICPC-Algorithms
master

搜索帮助