Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Added python solution for ContainerWIthMostWater problem (#537)
Browse files Browse the repository at this point in the history
* Added Soluion of ContainweWithMostWater problem

* Added Example

Signed-off-by: Pranjal Goyal <pranjalgoyal13@gmail.com>
  • Loading branch information
pranjalg13 authored Oct 12, 2021
1 parent 9d13c2d commit 147edc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Question: 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 the line i is at (i, ai) and (i, 0).
Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
Given Example
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
"""


def max_capacity(height):
max_area = 0

i = 0
j = len(height) - 1

# Using the two pointers namely i and j
while i < j:
curr_area = (j - i) * min(height[i], height[j])
max_area = max(max_area, curr_area)
if height[i] <= height[j]:
i = i + 1
else:
j = j - 1
return max_area

def main():
height = list(map(int,input().split(" ")))
print(max_capacity(height))


if __name__ == '__main__':
main()

#Time Complexity = O(n)
#Space Complexity = O(1)
29 changes: 0 additions & 29 deletions Programming/Python/ContainerWithMostWater/container.py

This file was deleted.

0 comments on commit 147edc1

Please sign in to comment.