-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathLargest_Rectangular_Area_in_a_Histogram.java
45 lines (37 loc) · 1.39 KB
/
Largest_Rectangular_Area_in_a_Histogram.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
package Algos;
import java.util.Scanner;
public class maxHistogramArea {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
/*input number of bars in the histogram*/
int n = scn.nextInt();
long[] a = new long[n];
/*input height of each bar*/
for (int i = 0; i < a.length; i++) {
a[i] = scn.nextLong();
}
System.out.println(solve(a));
}
public static long solve(long[] a) {
/*final max rectangular area to be returned*/
long ans = 0;
/*considering every bar as a starting point of an area*/
for (int i = 0; i < a.length; i++) {
/*for keeping track of shortest bar encountered yet
* when 'i' is chosen as the starting point of an area*/
long min = Integer.MAX_VALUE;
/* area when when 'i' is chosen as the starting point
* of an area*/
long rv=0;
for (int j = i; j < a.length; j++) {
/*current area will be the height of the shortest
* bar multiplied with the number of bars*/
min = Math.min(min, a[j]);
rv = Math.max(rv, min * (j - i+1));
}
/*updating overall max with the current max*/
ans = Math.max(ans, rv);
}
return ans;
}
}