## 题目地址

https://leetcode.com/problems/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.
``````

``````
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49
``````

## 思路

eg:

``````   // 这个解法比较暴力，效率比较低
// 时间复杂度是O(n^2)
let max = 0;
for(let i = 0; i < height.length; i++) {
for(let j = i + 1; j < height.length; j++) {
const currentArea = Math.abs(i - j) * Math.min(height[i], height[j]);
if (currentArea > max) {
max = currentArea;
}
}
}
return max;

``````

• 双指针优化时间复杂度

## 代码

• 语言支持：JS，C++

JavaScript Code:

``````/*
* @lc app=leetcode id=11 lang=javascript
*
* [11] Container With Most Water
*
* https://leetcode.com/problems/container-with-most-water/description/
*
* algorithms
* Medium (42.86%)
* Total Accepted:    344.3K
* Total Submissions: 790.1K
* Testcase Example:  '[1,8,6,2,5,4,8,3,7]'
*
* 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.
*
*
*
*
*
* The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In
* this case, the max area of water (blue section) the container can contain is
* 49.
*
*
*
* Example:
*
*
* Input: [1,8,6,2,5,4,8,3,7]
* Output: 49
*
*/
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
if (!height || height.length <= 1) return 0;

// 双指针来进行优化
// 时间复杂度是O(n)
let leftPos = 0;
let rightPos = height.length - 1;
let max = 0;
while(leftPos < rightPos) {

const currentArea = Math.abs(leftPos - rightPos) * Math.min(height[leftPos] , height[rightPos]);
if (currentArea > max) {
max = currentArea;
}
// 更新小的
if (height[leftPos] < height[rightPos]) {
leftPos++;
} else { // 如果相等就随便了
rightPos--;
}
}

return max;
};
``````

C++ Code:

``````class Solution {
public:
int maxArea(vector<int>& height) {
auto ret = 0ul, leftPos = 0ul, rightPos = height.size() - 1;
while( leftPos < rightPos)
{
ret = std::max(ret, std::min(height[leftPos], height[rightPos]) * (rightPos - leftPos));
if (height[leftPos] < height[rightPos]) ++leftPos;
else --rightPos;
}
return ret;
}
};
``````

### 代码2

C++ 解法一：

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

``````

Java 解法一：

``````public class Solution {
public int maxArea(int[] height) {
int res = 0, i = 0, j = height.length - 1;
while (i < j) {
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
if (height[i] < height[j]) ++i;
else --j;
}
return res;
}
}

``````

C++ 解法二：

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

``````

Java 解法二：

``````public class Solution {
public int maxArea(int[] height) {
int res = 0, i = 0, j = height.length - 1;
while (i < j) {
int h = Math.min(height[i], height[j]);
res = Math.max(res, h * (j - i));
while (i < j && h == height[i]) ++i;
while (i < j && h == height[j]) --j;
}
return res;
}
}
``````

评论
0 评论