Time Cost
15min36s
Implementation
To find the largest rectangle area, we need to determine for each bar how far it can extend to the left and right without encountering a shorter bar. Using monotonic stacks, we can compute the Nearest Smaller to Left (NSL) and Nearest Smaller to Right (NSR) efficiently.
Code
- My Solution
class Solution { public: int largestRectangleArea(vector<int>& heights) { int n = heights.size(); vector<int> left(n), right(n); stack<int> st; // Nearest Smaller to Left for (int i = 0; i < n; i++) { while (!st.empty() && heights[st.top()] >= heights[i]) st.pop(); left[i] = st.empty() ? -1 : st.top(); st.push(i); } while (!st.empty()) st.pop(); // Nearest Smaller to Right for (int i = n - 1; i >= 0; i--) { while (!st.empty() && heights[st.top()] >= heights[i]) st.pop(); right[i] = st.empty() ? n : st.top(); st.push(i); } int maxArea = 0; for (int i = 0; i < n; i++) { int width = right[i] - left[i] - 1; maxArea = max(maxArea, heights[i] * width); } return maxArea; } };