Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 84 · Monotonic Stack – Contribution / Area

Largest Rectangle in Histogram

LeetCode 84 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Monotonic stack; width = i − stack_top − 1 when popping.

ಕಠಿಣArrayStackMonotonic StackRange Minimum/Maximum Query
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Largest Rectangle in Histogram (LeetCode 84) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Monotonic Stack – Contribution / Area ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Each bar's best rectangle extends left and right until it hits a shorter bar. A stack of increasing heights finds both limits: when a shorter bar arrives, the bars it pops know their right edge (now) and their left edge (the new stack top).

Approach

  1. 1

    Divide and conquer version: split at the minimum bar recursively.

  2. 2

    Stack version with a −1 sentinel.

  3. 3

    On a shorter (or equal) bar, pop and compute height × width.

  4. 4

    After the scan, flush the stack with right edge n.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Divide and conquerO(n log n) avg, O(n²) worst

Split around the minimum.

2
Monotonic stackAcceptedO(n) time · O(n) space

Pop to find boundaries.

Divide and conquerPythonO(n log n) avg, O(n²) worst
class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        def get_area(start,end):
            if start>end:
                return 0
            min_index = start
            for i in range(start,end+1):
                if heights[i]<heights[min_index]:
                    min_index = i
            cur_area = (end-start+1)*heights[min_index]
            return max(cur_area,get_area(start,min_index-1),get_area(min_index+1,end))
        return get_area(0,len(heights)-1)
Monotonic stackPythonO(n) time · O(n) space
class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        stack=[-1]
        max_area = 0
        n = len(heights)
        for i in range(len(heights)):
            while heights[i]<=heights[stack[-1]] and stack[-1]!=-1:
                cur_height = heights[stack.pop()]
                cur_width = i-stack[-1]-1
                max_area = max(max_area,cur_width*cur_height)
            stack.append(i)
            #till last to begining
        while stack[-1]!=-1:
            cur_height = heights[stack.pop()]
            cur_width = n-stack[-1]-1
            max_area = max(max_area,cur_width*cur_height)
        return max_area
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Largest Rectangle in Histogram — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Largest Rectangle in Histogram ಹಿಂದಿನ intuition ಏನು?

Each bar's best rectangle extends left and right until it hits a shorter bar. A stack of increasing heights finds both limits: when a shorter bar arrives, the bars it pops know their right edge (now) and their left edge (the new stack top).

Largest Rectangle in Histogram (LeetCode 84) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Divide and conquer version: split at the minimum bar recursively. 2. Stack version with a −1 sentinel. 3. On a shorter (or equal) bar, pop and compute height × width. 4. After the scan, flush the stack with right edge n.

Largest Rectangle in Histogram ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n). Space: O(n). (Accepted approach: Monotonic stack.)

Largest Rectangle in Histogram ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

The −1 sentinel gives the correct width at the left edge.

Largest Rectangle in Histogram ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Divide and conquer (O(n log n) avg, O(n²) worst): Split around the minimum. Monotonic stack (O(n) time · O(n) space): Pop to find boundaries.