-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle.java
More file actions
31 lines (30 loc) · 938 Bytes
/
rectangle.java
File metadata and controls
31 lines (30 loc) · 938 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
public class Solution {
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int max=0,ii;
if(height.length==0) return 0;
for( ii=0;ii<height.length;ii++)
if(height[ii]!=height[0]) break;
if(ii==height.length) return height.length*height[0];
for(int i=0;i<height.length;i++) {
int c=(1+left(i-1,height[i],height)+right(i+1,height[i],height))*height[i];
max=Math.max(max,c);
}
return max;
}
public int left(int i,int h,int[] height) {
int l=0;
for(;i>=0;i--)
if(height[i]>=h) l++;
else break;
return l;
}
public int right(int i,int h,int[] height) {
int l=0;
for(;i<height.length;i++)
if(height[i]>=h) l++;
else break;
return l;
}
}