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

Added python solution for ContainerWIthMostWater problem #537

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.