[LeetCode]11. Container With Most Water
题目描述
思路
两个指针从两边往中间移动,移动左边或者右边的判断条件就是当前位置两个值的大小
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0, j = height.size() - 1;
int res = 0;
while (i < j) {
res = max(res, (j - i) * min(height[j], height[i]));
if (height[i] < height[j])
++i;
else
--j;
}
return res;
}
};
int main() {
vector<int> nums = { 2,3,4,5,18,17,6};
Solution s;
cout << s.maxArea(nums) << endl;
system("pause");
return 0;
}