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

Start the Journey
N2I -2020.03.18

1. My first try


class Solution:
    def maxArea(self, height: List[int]) -> int:
        maxcon = min(height[0],height[-1])*(len(height)-1)
        i=0
        j=len(height)-1
        while j>i:
            if height[i]>height[j]:
                j=j-1
            else :
                i=i+1            
            maxcon=max(maxcon,min(height[i],height[j])*(j-i))
        return maxcon



2. A small improvement


class Solution:
    def maxArea(self, height: List[int]) -> int:
        maxcon = min(height[0],height[-1])*(len(height)-1)
        i=0
        j=len(height)-1
        while j>i:
            if height[i]>height[j]:
                curcon=height[j]*(j-i)
                j=j-1
            else :
                curcon=height[i]*(j-i)
                i=i+1
            if curcon>maxcon:
                maxcon=curcon
        return maxcon


Explanation:

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

Comments