Skip to content

Commit 23ad475

Browse files
committed
Container with most water.
1 parent 7022a03 commit 23ad475

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
3+
4+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
5+
6+
Return the maximum amount of water a container can store.
7+
8+
Notice that you may not slant the container.
9+
10+
 
11+
12+
Example 1:
13+
Input: height = [1,8,6,2,5,4,8,3,7]
14+
Output: 49
15+
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.
16+
17+
Example 2:
18+
Input: height = [1,1]
19+
Output: 1
20+
 
21+
22+
Constraints:
23+
- n == height.length
24+
- 2 <= n <= 10^5
25+
- 0 <= height[i] <= 10^4
26+
*/
27+
class Solution {
28+
func maxArea(_ height: [Int]) -> Int {
29+
var left = 0
30+
var right = height.count - 1
31+
var maxArea = (right - left) * min(height[left], height[right])
32+
while left < right {
33+
left = 0
34+
let currentArea = (right - left) * min(height[left], height[right])
35+
if currentArea > maxArea {
36+
maxArea = currentArea
37+
}
38+
while left < right {
39+
if height[left] >= height[right] {
40+
let currentArea = (right - left) * min(height[left], height[right])
41+
if currentArea > maxArea {
42+
maxArea = currentArea
43+
}
44+
break
45+
}
46+
let currentArea = (right - left) * min(height[left], height[right])
47+
if currentArea > maxArea {
48+
maxArea = currentArea
49+
}
50+
left += 1
51+
}
52+
right -= 1
53+
}
54+
return maxArea
55+
}
56+
}
57+
58+
let s = Solution()
59+
let r = s.maxArea([10,9])
60+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Medium/11.Container With Most Water.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,5 @@
111111
4. [ZigZag Conversion](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/6.ZigZag%20Conversion.playground/Contents.swift)
112112
5. [Reverse Integer](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/7.Reverse%20Integer.playground/Contents.swift)
113113
6. [String to Integer (atoi)](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/8.String%20to%20Integer%20(atoi).playground/Contents.swift)
114+
7. [Container With Most Water](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/11.Container%20With%20Most%20Water.playground/Contents.swift)
114115

0 commit comments

Comments
 (0)