You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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])`.
14
+
15
+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
16
+
17
+
Return the maximum amount of water a container can store.
18
+
19
+
**Notice** that you may not slant the container.
20
+
21
+
**Example 1:**<br>
22
+
*Input:* `height = [1,8,6,2,5,4,8,3,7]` <br>
23
+
*Output:* `49` <br>
24
+
*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.
### [Container With Most Water](https://github.com/lgreydev/Help/blob/master/Help/LeetCode/ContainerWithMostWater.swift)
1562
+
1563
+
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])`.
1564
+
1565
+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
1566
+
1567
+
Return the maximum amount of water a container can store.
1568
+
1569
+
**Notice** that you may not slant the container.
1570
+
1571
+
**Example 1:**<br>
1572
+
*Input:* `height = [1,8,6,2,5,4,8,3,7]` <br>
1573
+
*Output:* `49` <br>
1574
+
*Explanation:* The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area ofwater (blue section) the container can contain is49.
1575
+
1576
+
**Example 2:**<br>
1577
+
*Input:* `height = [1,1]`<br>
1578
+
*Output:* `1`<br>
1579
+
1580
+
``` swift
1581
+
1582
+
funcmaxArea(_ height: [Int]) ->Int {
1583
+
guard!height.isEmptyelse { return-1 }
1584
+
1585
+
var maxArea =0
1586
+
var left =0
1587
+
var right = height.count-1
1588
+
1589
+
while left < right {
1590
+
// Re-calc maxArea
1591
+
let minHeight =min(height[left], height[right])
1592
+
let currentHeight = minHeight * (right - left)
1593
+
maxArea =max(maxArea, currentHeight)
1594
+
1595
+
// Move pointers
1596
+
if height[left] < height[right] {
1597
+
left +=1
1598
+
} else {
1599
+
right -=1
1600
+
}
1601
+
}
1602
+
return maxArea
1603
+
}
1604
+
1605
+
let input = [1, 8, 6, 2, 5, 4, 8, 3, 7]
1606
+
let result =maxArea(input)
1607
+
//print("Result: \(result)")
1608
+
1609
+
```
1610
+
1611
+
1561
1612
### 🛡️ License
1562
1613
1563
1614
This project is licensed under the MIT License - see the [`LICENSE`](https://github.com/lgreydev/Help/blob/master/License) file for details.
0 commit comments