LeetCode 11. Container With Most Water

LeetCode 11. Container With Most Water:题目、思路与代码实现。

题目描述

Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant(倾斜) the container and n is at least 2.

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution
{
public:
int maxArea(vector<int> &height)
{
int size = height.size();
int max = 0;
for (int i = 0; i < (size - 1); ++i)
{
for (int e = i + 1; e < size; ++e)
{
int temp = (height[i] > height[e]) ? height[e] : height[i];
// cout << "\t" << i << "\t" << e << "\t" << temp << "\t" << (e - i) * temp << endl;
if ((e - i) * temp > max)
max = (e - i) * temp;
}
}
return max;
}
};

本文最初发布于简书。

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2015-2026 Jeff
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信