8000 add 84 · deepakavs/Leetcode@742287d · GitHub
[go: up one dir, main page]

Skip to content

Commit 742287d

Browse files
add 84
1 parent 62cd2a6 commit 742287d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ Your ideas/fixes/algorithms are more than welcome!
443443
|87|[Scramble String](https://leetcode.com/problems/scramble-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_87.java)|O(?) |O(?)|Hard| Recursion
444444
|86|[Partition List](https://leetcode.com/problems/partition-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PartitionList.java)|O(?) |O(?)|Medium|
445445
|85|[Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MaximalRectangle.java)|O(m*n) |O(n)|Hard|DP
446+
|84|[Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_84.java)|O(n) |O(n)|Hard|Array, Stack
446447
|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_83.java)|O(n) |O(1)|Medium| Linked List
447448
|82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_82.java)|O(n) |O(1)|Medium| Linked List
448449
|81|[Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/SearchinRotatedSortedArrayII.java)|O(logn)|O(1)|Medium|Binary Search
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 84. Largest Rectangle in Histogram
7+
*
8+
* Given n non-negative integers representing the histogram's bar height where
9+
* the width of each bar is 1, find the area of largest rectangle in the histogram.
10+
11+
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
12+
13+
14+
The largest rectangle is shown in the shaded area, which has area = 10 unit.
15+
16+
For example,
17+
Given heights = [2,1,5,6,2,3],
18+
return 10.
19+
*/
20+
public class _84 {
21+
22+
/**credit: https://leetcode.com/articles/largest-rectangle-histogram/#approach-5-using-stack-accepted
23+
* and https://discuss.leetcode.com/topic/7599/o-n-stack-based-java-solution*/
24+
public int largestRectangleArea(int[] heights) {
25+
int len = heights.length;
26+
Stack<Integer> s = new Stack<>();
27+
int maxArea = 0;
28+
for (int i = 0; i <= len; i++) {
29+
int h = (i == len ? 0 : heights[i]);
30+
if (s.isEmpty() || h >= heights[s.peek()]) {
31+
s.push(i);
32+
} else {
33+
int tp = s.pop();
34+
maxArea = Math.max(maxArea, heights[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
35+
i--;
36+
}
37+
}
38+
return maxArea;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)
0