Skip to content

Commit 25cca29

Browse files
refactor 84
1 parent 94ee283 commit 25cca29

File tree

1 file changed

+20
-15
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-15
lines changed

src/main/java/com/fishercoder/solutions/_84.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,28 @@
1919
*/
2020
public class _84 {
2121

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*/
22+
public static class Solution1 {
23+
24+
/**
25+
* credit: https://leetcode.com/articles/largest-rectangle-histogram/#approach-5-using-stack-accepted
26+
* and https://discuss.leetcode.com/topic/7599/o-n-stack-based-java-solution
27+
*/
2428
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-
}
29+
int len = heights.length;
30+
Stack<Integer> s = new Stack<>();
31+
int maxArea = 0;
32+
for (int i = 0; i <= len; i++) {
33+
int h = (i == len ? 0 : heights[i]);
34+
if (s.isEmpty() || h >= heights[s.peek()]) {
35+
s.push(i);
36+
} else {
37+
int tp = s.pop();
38+
maxArea = Math.max(maxArea, heights[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
39+
i--;
3740
}
38-
return maxArea;
41+
}
42+
return maxArea;
3943
}
44+
}
4045

4146
}

0 commit comments

Comments
 (0)