Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a393c53

Browse files
added Container WIth Most Water (Medium)
1 parent 412d172 commit a393c53

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Container With Most Water
2+
3+
[Leetcode Link](https://leetcode.com/problems/container-with-most-water/)
4+
5+
## Problem:
6+
7+
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.
8+
9+
_Notice_ that you may not slant the container.
10+
11+
## Example:
12+
13+
![example](asset/question_11.jpg)
14+
15+
```
16+
Input: height = [1,8,6,2,5,4,8,3,7]
17+
Output: 49
18+
Explanation: 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.
19+
```
20+
21+
```
22+
Input: height = [4,3,2,1,4]
23+
Output: 16
24+
```
25+
26+
## Note:
17.9 KB
Loading[フレーム]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import List
2+
3+
# Recursively find all combinations (ie. brute force) is not efficient
4+
5+
6+
class Solution:
7+
def maxArea(self, height: List[int]) -> int:
8+
9+
# recursively find all combinations
10+
# def combinationOfTwo(arr, start=0, comb=[]):
11+
# if len(comb) == 2:
12+
# combinations.append(list(comb))
13+
# return
14+
# for i in range(start, len(arr)):
15+
# comb.append(i)
16+
# combinationOfTwo(arr, i+1, comb)
17+
# comb.pop()
18+
19+
# find height given two indices for containers
20+
def waterContainer(arr, left, right):
21+
height = min(arr[left], arr[right])
22+
width = right-left
23+
return height*width
24+
25+
# combinations = list()
26+
result = 0
27+
# combinationOfTwo(height)
28+
# for left, right in combinations:
29+
# result = max(result, waterContainer(height, left, right))
30+
31+
left = 0
32+
right = len(height)-1
33+
while left < right:
34+
result = max(result, waterContainer(height, left, right))
35+
if height[left] < height[right]:
36+
left += 1
37+
elif height[left] >= height[right]:
38+
right -= 1
39+
return result
40+
41+
42+
sol = Solution()
43+
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
44+
print(sol.maxArea(height))

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Languages used: Java and Python
9696
- [Permutations](Medium/Permutations)
9797
- [Permutations II](Medium/Permutations2)
9898
- [Combinations](Medium/Combinations)
99+
- [Container With Most Water](Medium/ContainerWithMostWater)
99100
- Hard
100101

101102
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
@@ -107,6 +108,7 @@ Languages used: Java and Python
107108
- [Escape a Large Maze](Hard/EscapeLargeMaze)
108109
- [Serialize and Deserialize Binary Tree](Hard/SerializeAndDeserializeBinaryTree)
109110
- [Permutation Sequence](Hard/PermutationSequence)
111+
- [Regular Expression Matching](Hard/RegexMatching)
110112

111113
- Others (Non-leetcode)
112114
- [Check Valid Tree Given Edges](Others/CheckValidTreeGivenEdges)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /