[LeetCode][python3]0011. Container With Most Water

Start the Journey
N2I -2020.03.18

1. My first try


  1. class Solution:
  2. def maxArea(self, height: List[int]) -> int:
  3. maxcon = min(height[0],height[-1])*(len(height)-1)
  4. i=0
  5. j=len(height)-1
  6. while j>i:
  7. if height[i]>height[j]:
  8. j=j-1
  9. else :
  10. i=i+1
  11. maxcon=max(maxcon,min(height[i],height[j])*(j-i))
  12. return maxcon
  13.  


2. A small improvement


  1. class Solution:
  2. def maxArea(self, height: List[int]) -> int:
  3. maxcon = min(height[0],height[-1])*(len(height)-1)
  4. i=0
  5. j=len(height)-1
  6. while j>i:
  7. if height[i]>height[j]:
  8. curcon=height[j]*(j-i)
  9. j=j-1
  10. else :
  11. curcon=height[i]*(j-i)
  12. i=i+1
  13. if curcon>maxcon:
  14. maxcon=curcon
  15. return maxcon
  16.  

Explanation:

In this improvement, we reduce min() and max() in every while loop, this makes the script runs obviously faster.