-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.盛最多水的容器.cpp
55 lines (53 loc) · 1.44 KB
/
11.盛最多水的容器.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode.cn id=11 lang=cpp
*
* [11] 盛最多水的容器
*
* https://leetcode-cn.com/problems/container-with-most-water/description/
*
* algorithms
* Medium (57.69%)
* Likes: 794
* Dislikes: 0
* Total Accepted: 79.8K
* Total Submissions: 138.4K
* Testcase Example: '[1,8,6,2,5,4,8,3,7]'
*
* 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为
* (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
*
* 说明:你不能倾斜容器,且 n 的值至少为 2。
*
*
*
* 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
*
*
*
* 示例:
*
* 输入: [1,8,6,2,5,4,8,3,7]
* 输出: 49
*
*/
class Solution {
public:
int maxArea(vector<int>& height) {
int s=0;
int start=0, end=height.size()-1;
while(start!=end){
if(height[start]<=height[end]) {
s = max(height[start]*(end-start),s);
start++;
}
else {
s = max(height[end]*(end-start),s);
end--;
}
}
return s;
}
int max(int x, int y){
return x>y?x:y ;
}
};