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.