forked from the-moonLight0/Hactober-fest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestHistogram.java
50 lines (46 loc) · 1.65 KB
/
LargestHistogram.java
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
package stack;
import java.util.Stack;
// problem link : https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram-1587115620/1/?track=SPC-Stack&batchId=154
public class LargestHistogramRectangle {
public static long getMaxAreaNaive(long[] arr, long n) {
//time complexity O(n^2)
// space complexity O(1)
long ans = 0L;
for(int i=0;i<n;i++){
int j=i+1;
while(j<n && arr[j]>=arr[i])j++;
int k = i-1;
while(k>=0 && arr[k]>=arr[i])k--;
long total = (j-i)*arr[i]+ (i-k-1)*arr[i];
ans = Math.max(ans , total);
}
return ans;
}
public static long getMaxAreaEfficient(long arr[], long n) {
// time complexity O(5n)
// space complexity O(3n)
Stack<Integer> leftSmall = new Stack<>();
int[] res = new int[(int)n];
for(int i=0;i<(int)n;i++){
while(!leftSmall.isEmpty() && arr[leftSmall.peek()]>=arr[i])
leftSmall.pop();
if(leftSmall.isEmpty())res[i] = i+1;
else res[i] = i-leftSmall.peek();
leftSmall.push(i);
}
Stack<Integer> rightSmall = new Stack<>();
for(int i=(int)n-1;i>=0;i--){
while(!rightSmall.isEmpty() && arr[rightSmall.peek()]>=arr[i])
rightSmall.pop();
if(rightSmall.isEmpty())res[i]+=n-i-1;
else res[i] += rightSmall.peek()-i-1;
rightSmall.push(i);
}
long max = 0L;
for(int i=0;i<n;i++){
long total = arr[i]*res[i];
max = Math.max(total,max);
}
return max;
}
}