forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f019818
commit b60d34c
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.util.Stack; | ||
|
||
public class LargestRectangle { | ||
public static void main(String[] args) { | ||
int[] arr = {2,1,5,6,2,3}; | ||
|
||
int res = largestRectangleArea(arr); | ||
System.out.println(res); | ||
|
||
|
||
} | ||
public static int largestRectangleArea(int[] heights) { | ||
int len = heights.length; | ||
Stack<Integer> s = new Stack<>(); | ||
int maxArea = 0; | ||
for (int i = 0; i <= len; i++){ | ||
int h = (i == len ? 0 : heights[i]); | ||
if (s.isEmpty() || h >= heights[s.peek()]) { | ||
s.push(i); | ||
} else { | ||
int top = s.pop(); | ||
maxArea = Math.max(maxArea, heights[top] * (s.isEmpty() ? i : i - 1 - s.peek())); | ||
i--; | ||
} | ||
} | ||
return maxArea; | ||
} | ||
} | ||
|
||
|
||
|