Skip to main content

Container with Most Water - Leetcode 11 - Python

 You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.


 Input: height = [1,8,6,2,5,4,8,3,7]

Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.


class Solution:

    def maxArea(self, height: List[int]) -> int:

        # ans = 0

        # for l in range(len(height)):

        #     for r in range(l+1, len(height)):

        #         ar = (r-l) * min(height[l],height[r])

        #         ans = max(ans, ar)

        # return ans

        

        ans = 0

        l, r = 0, len(height)-1

        

        while l<r:

            ar = (r-l) * min(height[l],height[r])

            ans = max(ans, ar)

            

            if height[l] < height[r]:

                l+=1

            else :

                r -=1

        return ans

            

             

  Explained :



   

Comments

Popular posts from this blog

Leetcode 190. Reverse Bits. Python. Bit Manipulation

  190 .  Reverse Bits Reverse bits of a given 32 bits unsigned integer. Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using  2's complement notation . Therefore, in  Example 2  above, the input represents the signed integer  -3  and the output represents the signed integer  -1073741825 .   Example 1: Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000 . Example 2: Input: n = 11111111111111111111...

Leetcode 417. Pacific Atlantic Water Flow. Python (Dfs Graph)

417 .  Pacific Atlantic Water Flow There is an  m x n  rectangular island that borders both the  Pacific Ocean  and  Atlantic Ocean . The  Pacific Ocean  touches the island's left and top edges, and the  Atlantic Ocean  touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an  m x n  integer matrix  heights  where  heights[r][c]  represents the  height above sea level  of the cell at coordinate  (r, c) . The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is  less than or equal to  the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean. Return  a  2D list  of grid coordinates  result  where  result[i] = [r i , c i ]  denotes that rain water can flow fro...

Leetcode 271. Encode and Decode Strings. Python (Easy)

Encode and Decode Strings Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Please implement  encode  and  decode Example Example1 Input: [ "lint" , "code" , "love" , "you" ] Output: [ "lint" , "code" , "love" , "you" ] Explanation: One possible encode method is: "lint:;code:;love:;you" Example2 Input: [ "we" , "say" , ":" , "yes" ] Output: [ "we" , "say" , ":" , "yes" ] Explanation: One possible encode method is: "we:;say:;:::;yes" Solution : class Solution:     def encode( self , strs):         ans = " "         for s in strs:             ans += str ( len (s)) + "$" + s         return ans     def decode( self , str ):         ans = []         i = 0                 w...