solution.md 4.2 KB
Newer Older
1 2
# 盛最多水的容器
<p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code>,</sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code> 和 <code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p><p><strong>说明:</strong>你不能倾斜容器。</p><p> </p><p><strong>示例 1:</strong></p><p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0011.Container%20With%20Most%20Water/images/question_11.jpg" style="height: 287px; width: 600px;" /></p><pre><strong>输入:</strong>[1,8,6,2,5,4,8,3,7]<strong><br />输出:</strong>49 <strong><br />解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [1,1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>height = [4,3,2,1,4]<strong><br />输出:</strong>16</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>height = [1,2,1]<strong><br />输出:</strong>2</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>n = height.length</code></li>	<li><code>2 <= n <= 3 * 10<sup>4</sup></code></li>	<li><code>0 <= height[i] <= 3 * 10<sup>4</sup></code></li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
3
<p>以下错误的选项是?</p>
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    int arr1[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};

    int length1 = sizeof(arr1) / sizeof(arr1[0]);

    vector<int> nums1(arr1, arr1 + length1);

    cout << sol.maxArea(nums1) << endl;
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    int maxArea(vector<int> &height)
    {
        int res = 0;
        for (int i = 0; i < height.size(); i++)
        {
            for (int j = i + 1; j < height.size(); j++)
            {
                int temp1 = max(height[i], height[j]);
                int temp2 = temp1 * (j - i);
                res = min(res, temp2);
            }
        }
        return res;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    int maxArea(vector<int> &height)
    {
        int i = 0;
        int j = height.size() - 1;
        int res = 0;
        while (i < j)
        {
            int temp1 = min(height[i], height[j]); 
            int temp2 = temp1 * (j - i);
            res = max(res, temp2);
            if (height[i] < height[j])
                i++;
            else
                j--;
        }
        return res;
    }
};
```

### B
```cpp
class Solution
{
public:
    int maxArea(vector<int> &height)
    {
        int tmp = 0, res = 0, max = 0;
        for (int i = 0; i < height.size(); ++i)
        {
            for (int j = i + 1; j < height.size(); ++j)
            {
                if (height[i] < height[j])
                {
                    tmp = height[i];
                }
                else
                {
                    tmp = height[j];
                }
                res = tmp * (j - i);
                if (res > max)
                {
                    max = res;
                }
            }
        }
        return max;
    }
};
```

### C
```cpp
class Solution
{
public:
    int maxArea(vector<int> &height)
    {
        int tail = height.size() - 1;
        int head = 0;
        int maxarea = height.at(head) > height.at(tail) ? height.at(tail) * (tail - head) : height.at(head) * (tail - head);
        for (int i = 0; i < height.size(); i++)
        {
            if (maxarea < (height.at(head) > height.at(tail) ? height.at(tail) * (tail - head) : height.at(head) * (tail - head)))
                maxarea = height.at(head) > height.at(tail) ? height.at(tail) * (tail - head) : height.at(head) * (tail - head);

            if (height.at(head) > height.at(tail))
            {
                tail--;
            }
            else
            {
                head++;
            }
        }
        return maxarea;
    }
};
```