Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 11 · Two Pointers

Container With Most Water

LeetCode 11 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Move the shorter line inward.

మధ్యస్థంArrayTwo PointersGreedy
LeetCode లో తెరవండి ↗

నేను Container With Most Water (LeetCode 11) ను ఇలా పరిష్కరించాను — Two Pointers ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Start with the widest container. Its height is limited by the shorter wall, and moving the taller wall inward can only shrink width without raising that limit — so always move the shorter wall and hope for a taller one.

Approach

  1. 1

    left = 0, right = n − 1.

  2. 2

    Area = min(height) × width; track the max.

  3. 3

    Move whichever pointer has the smaller height.

నేను ఎలా పరిష్కరించాను

Two pointersPythonO(n) time · O(1) space
class Solution:
    def maxArea(self, height: List[int]) -> int:
        left =0
        right = len(height)-1
        max_area = 0
        while left<right:
            min_height = min(height[left],height[right])
            max_area = max(max_area,min_height*(right-left))
            if height[left]<height[right]:
                left+=1
            else:
                right-=1
        return max_area
స్టెప్-త్రూ లోడ్ అవుతోంది…

Container With Most Water — తరచూ అడిగే ప్రశ్నలు

Container With Most Water వెనుక ఉన్న intuition ఏమిటి?

Start with the widest container. Its height is limited by the shorter wall, and moving the taller wall inward can only shrink width without raising that limit — so always move the shorter wall and hope for a taller one.

Container With Most Water (LeetCode 11) ను ఎలా పరిష్కరించాలి?

1. left = 0, right = n − 1. 2. Area = min(height) × width; track the max. 3. Move whichever pointer has the smaller height.

Container With Most Water యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(1). (Accepted approach: Two pointers.)

Container With Most Water లో సాధారణ పొరపాట్లు ఏమిటి?

Moving the taller side can never help — that's the proof of correctness.