代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。