-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem11.cpp
59 lines (55 loc) · 1.6 KB
/
problem11.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
56
57
58
/*
* @lc app=leetcode.cn id=11 lang=cpp
*
* [11] 盛最多水的容器
*
* https://leetcode-cn.com/problems/container-with-most-water/description/
*
* algorithms
* Medium (61.85%)
* Likes: 1192
* Dislikes: 0
* Total Accepted: 154.5K
* Total Submissions: 249.5K
* 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
*
*/
// @lc code=start
class Solution {
public:
int maxArea(vector<int>& height) {
int result = 0;
for(int left=0; left<height.size(); left++) {
for(int right=left; right<height.size(); right++) {
int A = Area(height, left, right);
result = (result>A)?result:A;
}
}
return result;
}
int Area(vector<int>& height, const int left, const int right) {
const int left_height = height[left];
const int right_height = height[right];
const int min_height = (left_height<right_height)?left_height:right_height;
return (right - left) * min_height;
}
};
// @lc code=end