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.