본문 바로가기

00/algorithm

Container With Most Water (in Python)

반응형
Problem

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, O). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note : You may not slant the container and n is at least 2.


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.


Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

Solution

• To maximize the Area that can be formed between the vertical lines using the shorter line as length and the distance between the lines as the width of the rectangle forming the area.



Approach 1 : Brute Force


In this case, we will simply consider the area for every possible pair of the lines and find out the maximum area out of those.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
 
        # 1. Brute Force
        max_water = 0
        n = len(height)
 
        for i in range(n) :
            for j in range(i+1, n) :
                max_water = max((j-i)*min(height[i], height[j]), max_water)
 
        return max_water
cs


Complexity Analysis

• Time complexity : \(O(n^2)\). Calculating area for all \(n(n-1)/2\) height pairs.

• Space complexity : \(O(1)\). Constant extra space is used.




Approach 2: Two Pointer Approach


Algorithm


The intuition behind this approach is that the area formed between the lines will always be limited by the height of the shorter line. Further, the father the lines, the more will be the area obtained.


We take two pointers, one at the beginning and one at the end of the array constituting the length of the lines. Further, we maintain a variable \(maxarea\) to store the maximum area obtained till now. At every step, we find out the area formed between them, update \(maxarea\) and move the pointer pointing to the shorter line towards the other and by one step.


Current

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        
        left, right, y = 0len(height)-10
        max_water = 0
 
        while left < right :
 
            x = (j-i)
 
            if height[left]<=height[right] :
                y = height[left]
                left+=1
            else :
                y = height[right]
                right -= 1
            
            max_water = max(x * y, ans)
 
        return ans
cs


Complexity Analysis

• Time complexity : \(O(n)\). Single pass.

• Space complexity : \(O(n)\). Constant space is used.



Source : https://leetcode.com/problems/container-with-most-water


반응형

'0 > algorithm' 카테고리의 다른 글

Letter Combinations of a Phone Number (in Python)  (0) 2019.01.20
3Sum (in Python)  (2) 2019.01.20
ZigZag Conversion (in Python)  (0) 2019.01.20
Longest Palindromic Substring (in Python)  (0) 2019.01.19
Median of Two Sorted Arrays (in Python)  (0) 2019.01.19