编辑代码

package main
import "fmt"


 
func main()  {
    // 1,8,6,2,5,4,8,3,7
    // 1,2,1
    // 1,2
    // 1,2,4,3
    height := []int{1,8,6,2,5,4,8,3,7}
    fmt.Println(maxArea(height))
}


func maxArea(height []int) int {
    if len(height) == 0 {
        return 0
    }
    //双指针
    // 1,2,3,4
    // 1,8,6,2,5,4,8,3,7
    // 3,2,5,1
    // 目前第一步min(x,y) * t = x * t
    // 第二步,如果要挪动y, 
    //      y1 < y :  
    //               y1 > x ->  min(x,y1) * t1 = x * t1 < x * t
    //               y1 < x ->  min(x, y1) * t1 = y * t1 < y * t1

    // 第二步,第二种情况 如果挪动x,
    //      x1 > y :
    //             min(x1,y)*t1 = y * t1 
    //                y * t1 > x * t 
    //      x1 < y :
    //             min(x1,y)*t1 = x1 *t1
    //                x1 > x    x1 * t1 >= x * t
    //                x1 < x    x1 * t1 < x * t 
    left, right := 0, len(height) - 1

    var maxInt int
    for left < right {
        var baseMaxInt int 
        if height[left] <= height[right] {
            baseMaxInt = (right-left) * height[left]
            left++
        } else if height[left] > height[right] {
            baseMaxInt = (right-left) * height[right]
            right--
        }

        if baseMaxInt > maxInt {
            maxInt = baseMaxInt
        }
    }
    return maxInt
}