- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Container With Most Water in Python
Suppose we have a set of n non-negative integers a1, a2, ..., an,each value represents a point at coordinate (i, a[i]). n vertical lines are present in such a way that the two endpoints of line i is at (i, a[i]) and (i, a[0]). We have to find two lines, which together with x-axis forms one container, so our goal is to find two columns where water volume is max. So if the array is like [1,8,6,2,5,4,8,3,7], then it will be
In the shaded part, the height is 7 and there are 7 sections, so the total area is actually 7 * 7 = 49. This is the output.
To solve this, we will follow these steps
- low := 0, high := length of arr – 1, ans := 0
- while low < high
- if arr[low] < arr[high]: min_h := height[low] and min_ind := low
- otherwise min_h := height[high] and min_ind := high
- ans := maximum of (high – low)* min_h and ans
- if low + 1 = min_ind + 1, then increase low by 1 otherwise decrease high by 1
- return ans
Example
Let us see the following implementation to get better understanding −
class Solution(object): def maxArea(self, height): low = 0 high = len(height) - 1 ans = 0 while low < high: if height[low]<height[high]: min_height = height[low] min_height_index = low else: min_height = height[high] min_height_index = high ans = max(((high - low) ) * min_height,ans) if low+1==min_height_index+1: low+=1 else: high-=1 return ans ob1 = Solution() print(ob1.maxArea([1,8,6,2,5,4,8,3,7]))
Input
[1,8,6,2,5,4,8,3,7]
Output
49
- Related Articles
- Container with Most Water in C++
- Python Container Datatypes
- Working with Java inside Docker Container
- How to Identify Most Frequently Occurring Items in a Sequence with Python?
- Create a Flexbox container with Bootstrap 4
- Name the most common practice of recharging ground water.
- Python - Most common Combination in Matrix
- Container for form input and label with Bootstrap
- Program to find land with longest distance from water in Python
- How to disable a Tab in a JTabbedPane Container with Java?
- How does roughage fill up the most need for water in our body?
- A closed container contains moist air. How can the quantity of water in the air be reduced?
- Bootstrap Grid Stacked to horizontal grid with fluid container
- The correct way of making a solution of acid in water is to(a) add water to acid(b) add acid to water(c) mix acid and water simultaneously(d) add water to acid in a shallow container
- The most Common POSIX System Calls in Python

Advertisements