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.