|
26 | 26 |
|
27 | 27 | <!-- 这里可写通用的实现逻辑 -->
|
28 | 28 |
|
| 29 | +双指针解决。 |
| 30 | + |
| 31 | +一开始,我们考虑相距最远的两个柱子所能容纳水的面积。水的宽度是两根柱子之间的距离,而水的高度取决于两根柱子之间较短的那个。 |
| 32 | + |
| 33 | +- 当前柱子是最两侧的柱子,水的宽度最大,其他的组合,水的宽度都比这个小; |
| 34 | +- 当前左侧柱子较短,决定了水的高度。如果移动左侧的柱子,新的水面高度不确定,但一定不会超过右侧的柱子高度; |
| 35 | +- 如果移动右侧的柱子,新的水面高度一定不会超过左侧的柱子高度,也就是不会超过当前的水面高度。 |
| 36 | + |
| 37 | +可见,如果固定左侧的柱子,向内移动右侧的柱子,水的高度一定不会增加,且宽度一定减少,所以水的面积一定减少。所以左侧的柱子跟右侧其他柱子的组合,都可以排除了。也就是代码中的 `i++`。 |
| 38 | + |
| 39 | +移动左侧的柱子中,重复进行上面的操作。 |
| 40 | + |
| 41 | +在此过程中,我们不断排除掉无法成为构成最大值的柱子组合,而每一次都获取到可能为最大值的面积 t。那么遍历结束之后,我们就可以得到最大值。 |
| 42 | + |
29 | 43 | <!-- tabs:start -->
|
30 | 44 |
|
31 | 45 | ### **Python3**
|
32 | 46 |
|
33 | 47 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
34 | 48 |
|
35 | 49 | ```python
|
36 |
| - |
| 50 | +class Solution: |
| 51 | + def maxArea(self, height: List[int]) -> int: |
| 52 | + i, j = 0, len(height) - 1 |
| 53 | + res = 0 |
| 54 | + while i < j: |
| 55 | + t = (j - i) * min(height[i], height[j]) |
| 56 | + res = max(res, t) |
| 57 | + if height[i] < height[j]: |
| 58 | + i += 1 |
| 59 | + else: |
| 60 | + j -= 1 |
| 61 | + return res |
37 | 62 | ```
|
38 | 63 |
|
39 | 64 | ### **Java**
|
40 | 65 |
|
41 | 66 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
42 | 67 |
|
43 | 68 | ```java
|
| 69 | +class Solution { |
| 70 | + public int maxArea(int[] height) { |
| 71 | + int i = 0, j = height.length - 1; |
| 72 | + int res = 0; |
| 73 | + while (i < j) { |
| 74 | + int t = (j - i) * Math.min(height[i], height[j]); |
| 75 | + res = Math.max(res, t); |
| 76 | + if (height[i] < height[j]) ++i; |
| 77 | + else --j; |
| 78 | + } |
| 79 | + return res; |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### **C++** |
| 85 | + |
| 86 | +```cpp |
| 87 | +class Solution { |
| 88 | +public: |
| 89 | + int maxArea(vector<int>& height) { |
| 90 | + int i = 0, j = height.size() - 1; |
| 91 | + int res = 0; |
| 92 | + while (i < j) { |
| 93 | + int t = (j - i) * min(height[i], height[j]); |
| 94 | + res = max(res, t); |
| 95 | + if (height[i] < height[j]) ++i; |
| 96 | + else --j; |
| 97 | + } |
| 98 | + return res; |
| 99 | + } |
| 100 | +}; |
| 101 | +``` |
| 102 | +
|
| 103 | +### **Go** |
| 104 | +
|
| 105 | +```go |
| 106 | +func maxArea(height []int) int { |
| 107 | + i, j := 0, len(height) - 1 |
| 108 | + res := 0 |
| 109 | + for i != j { |
| 110 | + t := (j - i) * min(height[i], height[j]) |
| 111 | + res = max(res, t) |
| 112 | + if height[i] < height[j] { |
| 113 | + i++ |
| 114 | + } else { |
| 115 | + j-- |
| 116 | + } |
| 117 | + } |
| 118 | + return res |
| 119 | +} |
| 120 | +
|
| 121 | +func min(a, b int) int { |
| 122 | + if a > b { |
| 123 | + return b |
| 124 | + } |
| 125 | + return a |
| 126 | +} |
| 127 | +
|
| 128 | +func max(a, b int) int { |
| 129 | + if a > b { |
| 130 | + return a |
| 131 | + } |
| 132 | + return b |
| 133 | +} |
| 134 | +``` |
44 | 135 |
|
| 136 | +### **JavaScript** |
| 137 | + |
| 138 | +```js |
| 139 | +/** |
| 140 | + * @param {number[]} height |
| 141 | + * @return {number} |
| 142 | + */ |
| 143 | +var maxArea = function (height) { |
| 144 | + let i = 0, |
| 145 | + j = height.length - 1; |
| 146 | + let res = 0; |
| 147 | + while (i < j) { |
| 148 | + const t = (j - i) * Math.min(height[i], height[j]); |
| 149 | + res = Math.max(res, t); |
| 150 | + if (height[i] < height[j]) ++i; |
| 151 | + else --j; |
| 152 | + } |
| 153 | + return res; |
| 154 | +}; |
45 | 155 | ```
|
46 | 156 |
|
47 | 157 | ### **...**
|
|
0 commit comments