题解 | #盛水最多的容器#
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
#include <algorithm>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param height int整型vector
* @return int整型
*/
int maxArea(vector<int>& height) {
// write code here
// 找数组两个值之间的最小值,乘以他们的距离求水
// 求水最大的情况
int Asize = height.size();
if(Asize<2){
return 0;
}
vector<int> value;
int maxSum = 0;
// 不能排序,排序的话会改变值的位置
// 需要删除已经比较过的内容。
// 找两个最大的值作乘积
int head = 0;
int end = Asize-1;
while(head!=end && head<Asize && end>=0){
int tmp = min(height[head],height[end])*(end-head);
maxSum = max(maxSum, tmp);
if(height[head]>height[end]){
end --;
}
else{
head++;
}
}
return maxSum;
}
};
查看1道真题和解析