版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/glw0223/article/details/88431675
题面
给定 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
源码实现
func MaxArea(height []int) (result int) {
length:=len(height)
for i:=0;i<length;i++{
for j:=i+1;j<length;j++{
result = util.Max(result, util.Min(height[i],height[j])*(j-i))
}
}
return
}
func MaxArea1(height []int) (result int) {
length:=len(height)
left:=0
right:=length-1
for left<right{
result = util.Max(result, util.Min(height[left],height[right])*(right-left))
if height[left]<height[right]{
left++
}else {
right--
}
}
return
}
测试代码及结果
package question11_20
import (
"fmt"
"github.com/glw0223/LeetCode-go/question11_20"
"testing"
)
func TestMaxArea(t *testing.T) {
input:=[]int{1,8,6,2,5,4,8,3,7}
result:=question11_20.MaxArea(input)
fmt.Println("MaxArea","input:",input,"result:",result)
result=question11_20.MaxArea1(input)
fmt.Println("MaxArea1","input:",input,"result:",result)
}
API server listening at: 127.0.0.1:62541
=== RUN TestMaxArea
MaxArea input: [1 8 6 2 5 4 8 3 7] result: 49
MaxArea1 input: [1 8 6 2 5 4 8 3 7] result: 49
--- PASS: TestMaxArea (0.00s)
PASS
Debugger finished with exit code 0
复杂度
方法一
时间复杂度:
,计算所有
种高度组合的面积。
空间复杂度:
。
方法二
时间复杂度:
,只扫描了一次。
空间复杂度:
。