forked from prakhar1989/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.cpp
More file actions
63 lines (57 loc) · 1.35 KB
/
temp.cpp
File metadata and controls
63 lines (57 loc) · 1.35 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
int largestArea(int arr[], int len)
{
int area[len]; //initialize it to 0
int n, i, t;
stack<int> St; //include stack for using this #include<stack>
bool done;
for (i=0; i<len; i++)
{
while (!St.empty())
{
if(arr[i] <= arr[St.top()])
{
St.pop();
}
else
break;
}
if(St.empty())
t = -1;
else
t = St.top();
//Calculating Li
area[i] = i - t - 1;
St.push(i);
}
//clearing stack for finding Ri
while (!St.empty())
St.pop();
for (i=len-1; i>=0; i--)
{
while (!St.empty())
{
if(arr[i] <= arr[St.top()])
{
St.pop();
}
else
break;
}
if(St.empty())
t = len;
else
t = St.top();
//calculating Ri, after this step area[i] = Li + Ri
area[i] += t - i -1;
St.push(i);
}
int max = 0;
//Calculating Area[i] and find max Area
for (i=0; i<len; i++)
{
area[i] = arr[i] * (area[i] + 1);
if (area[i] > max)
max = area[i];
}
return max;
}