nathanwailes

LeetCode 11 - Container With Most Water - 2022.12.31 solution

Dec 31st, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. class Solution:
  2.     def maxArea(self, height: List[int]) -> int:
  3.         """ How to solve it: You can have two pointers advancing from the outside in.  The tricky thing is
  4.        knowing how to decide which pointer to advance inwards.  I think the key insight is to realize that any
  5.        *better* combination that you can find by moving inwards *must* involve the *taller* of the two walls
  6.        staying, while the *shorter* of the two walls moves inwards.  The shorter wall is the limiting factor
  7.        that limits the current water and would continue to limit any taller wall on the other side.
  8.        """
  9.         l = 0
  10.         r = len(height) - 1
  11.         max_water = 0
  12.         while l < r:
  13.             max_water = max(max_water, (r - l) * min(height[l], height[r]))
  14.             if height[l] <= height[r]:
  15.                 l += 1
  16.             else:
  17.                 r -= 1
  18.         return max_water
Advertisement
Add Comment
Please, Sign In to add comment