當左端線段L小於右端線段R時,咱們把L右移,這時捨棄的是L與右端其餘線段(R-1, R-2, ...)組成的木桶,這些木桶是不必判斷的,由於這些木桶的容積確定都沒有L和R組成的木桶容積大。code
public class Solution { public int maxArea(int[] height) { int p = 0; int q = height.length - 1; int max = 0; int tempMax = 0; while (p < q) { if (height[p] < height[q]) { tempMax = (q - p) * height[p]; if (tempMax > max) { max = tempMax; } p++; } else { tempMax = (q - p) * height[q]; if (tempMax > max) { max = tempMax; } q--; } } return max; } }
自責本身爲何沒作出來io