Skip to content

Commit b80c50c

Browse files
authored
Update 84-largest-rectangle-in-histogram.js
1 parent 82273c5 commit b80c50c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

84-largest-rectangle-in-histogram.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,24 @@ const largestRectangleArea = function(heights) {
5858
}
5959
return max;
6060
};
61+
62+
// another
63+
64+
/**
65+
* @param {number[]} heights
66+
* @return {number}
67+
*/
68+
const largestRectangleArea = function(heights) {
69+
heights.push(0)
70+
const st = [], n = heights.length
71+
let res = 0
72+
for(let i = 0; i <= n; i++) {
73+
while(st.length && heights[st[st.length - 1]] >= heights[i]) {
74+
const top = st.pop()
75+
const pre = st.length ? st[st.length - 1] : -1
76+
res = Math.max(res, heights[top] * (i - pre - 1))
77+
}
78+
st.push(i)
79+
}
80+
return res
81+
};

0 commit comments

Comments
 (0)