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.