[Swift]LeetCode11. 盛最多水的容器 | Container With Most Water
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9883829.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
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.
Example:
Input: [1,8,6,2,5,4,8,3,7] Output: 49
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
双指针法
这种方法背后的思路在于,两线段之间形成的区域总是会受到其中较短那条长度的限制。此外,两线段距离越远,得到的面积就越大。
我们在由线段长度构成的数组中使用两个指针,一个放在开始,一个置于末尾。 此外,我们会使用变量 maxareamaxarea 来持续存储到目前为止所获得的最大面积。 在每一步中,我们会找出指针所指向的两条线段形成的区域,更新 maxareamaxarea,并将指向较短线段的指针向较长线段那端移动一步。
查看下面的例子将有助于你更好地理解该算法:1 8 6 2 5 4 8 3 7
这种方法如何工作?
最初我们考虑由最外围两条线段构成的区域。现在,为了使面积最大化,我们需要考虑更长的两条线段之间的区域。如果我们试图将指向较长线段的指针向内侧移动,矩形区域的面积将受限于较短的线段而不会获得任何增加。但是,在同样的条件下,移动指向较短线段的指针尽管造成了矩形宽度的减小,但却可能会有助于面积的增大。因为移动较短线段的指针会得到一条相对较长的线段,这可以克服由宽度减小而引起的面积减小。
120ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 var maxarea:Int = 0, l:Int = 0, r:Int = height.count - 1 4 while(l < r) 5 { 6 maxarea = max(maxarea, min(height[l], height[r]) * (r - l)) 7 if height[l] < height[r] 8 { 9 l += 1 10 } 11 else 12 { 13 r -= 1 14 } 15 } 16 return maxarea 17 } 18 }
20ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 var left = 0 4 var right = height.count - 1 5 var maxArea = area(height, left, right) 6 while(left < right) { 7 if (height[left] < height[right]) { 8 left = left + 1 9 } else { 10 right = right - 1 11 } 12 let lArea = area(height, left, right) 13 if (lArea > maxArea) { 14 maxArea = lArea 15 } 16 } 17 return maxArea 18 } 19 20 func area(_ height: [Int], _ start: Int, _ end: Int) -> Int { 21 let x = height[start] 22 let y = height[end] 23 return (end - start) * (x > y ? y : x) 24 } 25 26 }
20ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 var result = 0 4 5 var left = 0; 6 var right = height.count - 1; 7 8 while left < right { 9 var valleft = height[left] 10 var valright = height[right] 11 let area = (valleft > valright ? valright : valleft) * (right - left) 12 13 if area > result { 14 result = area 15 } 16 17 if valleft > valright { 18 right -= 1 19 } else { 20 left += 1 21 } 22 } 23 return result 24 } 25 }
24ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 var ans = 0 4 5 var i = 0 6 var j = height.count - 1 7 8 while i < j { 9 let h1 = height[i] 10 let h2 = height[j] 11 12 let area = abs(i - j) * min(h1, h2) 13 ans = max(ans, area) 14 15 if h1 < h2 { i += 1 } 16 else { j -= 1 } 17 } 18 19 return ans 20 } 21 }
28ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 var area = 0 4 var left = 0 5 var right = 0 6 7 var i = 0 8 var j = height.count - 1 9 10 while i < j { 11 if left <= right { 12 let tmpArea = left * (j - i) 13 area = area < tmpArea ? tmpArea : area 14 if left < height[i] { 15 left = height[i] 16 } else { 17 i += 1 18 } 19 } else { 20 let tmpArea = right * (j - i) 21 area = area < tmpArea ? tmpArea : area 22 if right < height[j] { 23 right = height[j] 24 } else { 25 j -= 1 26 } 27 } 28 } 29 return area 30 } 31 }
40ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 var result = 0 4 var i = 0 5 var j = height.count - 1 6 7 while (i < j) { 8 let minHeight = min(height[i], height[j]) 9 let area = minHeight * (j - i) 10 result = max(result, area) 11 while (height[i] <= minHeight && i < j) { i += 1 } 12 while (height[j] <= minHeight && i < j) { j -= 1 } 13 print(i, j) 14 } 15 return result 16 } 17 }
108ms
1 class Solution { 2 func maxArea(_ height: [Int]) -> Int { 3 if height.count == 0 { return 0 } 4 if height.count == 1 { return height[0] } 5 var head = 0 6 var tail = height.count - 1 7 let sortFunc: (((Int, Int), (Int, Int)) -> Bool) = { (ele1, ele2) -> Bool in 8 return ele1.1 < ele2.1 9 } 10 11 var maxArea = 0 12 for (idx, h) in height.enumerated().sorted(by: sortFunc) { 13 while h > height[head] { head += 1 } 14 while h > height[tail] { tail -= 1 } 15 16 let dToHead = idx - head 17 let dToTail = tail - idx 18 let area = max( dToHead * h, dToTail * h ) 19 if area > maxArea { 20 maxArea = area 21 } 22 } 23 24 return maxArea 25 } 26 }