This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added python solution for ContainerWIthMostWater problem (#537)
* Added Soluion of ContainweWithMostWater problem * Added Example Signed-off-by: Pranjal Goyal <pranjalgoyal13@gmail.com>
- Loading branch information
1 parent
9d13c2d
commit 147edc1
Showing
2 changed files
with
38 additions
and
29 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Programming/Python/ContainerWithMostWater/ContainerWithMostWater.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.