leet code 11 Container With Most Water golang解題
阿新 • • 發佈:2018-12-16
理解題意:一個數組i,a[i]形成一個高度,找到兩個高度之間能裝最多水的面積。
思路:
- 從兩邊向中間找(不考慮高度的情況下,長度最大)
- 從比較小的開始向中間找比他大的(比他小的容積不可能更大)
func maxArea(height [] int) int { length := len(height) i := 0 j := length - 1 lh := height[0] rh := height[length - 1] max := 0 for i < j { tmp := minInt(lh,rh) * (j - i) if tmp > max { max = tmp } //右邊大 if lh < rh { for i < j && height[i] <= lh { i++ } fmt.Println("1----",i,j) if height[i] >= lh { lh = height[i]; } }else{ for i < j && height[j] <= rh { j-- } fmt.Println("2----",i,j) if height[j] > rh { rh = height[j]; } } } return max; } func minInt (int1 int , int2 int) int { if int1 > int2 { return int2 }else{ return int1 } }