-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestAreaInHistogram.cpp
More file actions
42 lines (34 loc) · 893 Bytes
/
largestAreaInHistogram.cpp
File metadata and controls
42 lines (34 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
// Largest area in histogram
void naive(){
vector<int> a = {2,1,5,6,2,3};
int ans = 0;
for(int i = 0; i < a.size(); i++){
// in every iteration every element being the smallest element;
int sum = a[i];
// check left if any element is greater or equal to the a[i] then add a[i] to the sum;
for(int j = i-1; j >= 0; j--){
if(a[j] >= a[i]){
sum += a[i];
}else{
break;
}
}
// check right the same way
for(int k = i+1; k < a.size(); k++){
if(a[k] >= a[i]){
sum += a[i];
}else{
break;
}
}
ans = max(ans, sum);
}
cout << ans << '\n';
}
int main(){
vector<int> a = {6, 2, 5, 4, 1, 5, 6};
naive();
return 0;
}