11.Container With Most Water

ss
Jan 23, 2021

--

這題就是看最大可以裝多少水, 我們用指標從兩邊開始靠近, 我們去比較兩邊誰高, 肯定是低的那個人要移動, 在移動的途中, 若是發現更低的人, 要直接跳過, 因為距離變更短, 高度變更低, 一定沒有機會裝更多水

class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0;
int r = height.size() -1;
int res = 0 ;
while(l < r){
res = max(min(height[r], height[l]) *(r-l), res);
if(height[l] >= height[r]){
r--;
}else{
l++;
}
}
return res;
}
};

--

--

ss
ss

No responses yet