Container With Most Water
LeetCode 11 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
Move the shorter line inward.
मैंने 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
left = 0, right = n − 1.
- 2
Area = min(height) × width; track the max.
- 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_areaContainer 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.