54
54
55
55
循环此过程,直到两个柱子相遇。
56
56
57
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。 其中 $n$ 是数组 ` height ` 的长度。
57
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 ` height ` 的长度。空间复杂度 $O(1)$ 。
58
58
59
59
<!-- tabs:start -->
60
60
66
66
class Solution :
67
67
def maxArea (self , height : List[int ]) -> int :
68
68
i, j = 0 , len (height) - 1
69
- res = 0
69
+ ans = 0
70
70
while i < j:
71
71
t = (j - i) * min (height[i], height[j])
72
- res = max (res , t)
72
+ ans = max (ans , t)
73
73
if height[i] < height[j]:
74
74
i += 1
75
75
else :
76
76
j -= 1
77
- return res
77
+ return ans
78
78
```
79
79
80
80
### ** Java**
@@ -85,16 +85,17 @@ class Solution:
85
85
class Solution {
86
86
public int maxArea (int [] height ) {
87
87
int i = 0 , j = height. length - 1 ;
88
- int res = 0 ;
88
+ int ans = 0 ;
89
89
while (i < j) {
90
- int t = (j - i) * Math . min(height[i], height[j]);
91
- res = Math . max(res , t);
92
- if (height[i] < height[j])
90
+ int t = Math . min(height[i], height[j]) * (j - i );
91
+ ans = Math . max(ans , t);
92
+ if (height[i] < height[j]) {
93
93
++ i;
94
- else
94
+ } else {
95
95
-- j;
96
+ }
96
97
}
97
- return res ;
98
+ return ans ;
98
99
}
99
100
}
100
101
```
@@ -106,50 +107,50 @@ class Solution {
106
107
public:
107
108
int maxArea(vector<int >& height) {
108
109
int i = 0, j = height.size() - 1;
109
- int res = 0;
110
+ int ans = 0;
110
111
while (i < j) {
111
- int t = (j - i) * min(height[ i] , height[ j] );
112
- res = max(res , t);
113
- if (height[ i] < height[ j] )
112
+ int t = min(height[ i] , height[ j] ) * (j - i );
113
+ ans = max(ans , t);
114
+ if (height[ i] < height[ j] ) {
114
115
++i;
115
- else
116
+ } else {
116
117
--j;
118
+ }
117
119
}
118
- return res ;
120
+ return ans ;
119
121
}
120
122
};
121
123
```
122
124
123
125
### **Go**
124
126
125
127
```go
126
- func maxArea(height []int) int {
127
- i, j := 0, len(height) - 1
128
- res := 0
129
- for i != j {
130
- t := (j - i) * min(height[i], height[j])
131
- res = max(res, t)
132
- if height[i] < height[j] {
133
- i++
134
- } else {
135
- j--
136
- }
137
- }
138
- return res
128
+ func maxArea(height []int) (ans int) {
129
+ i, j := 0, len(height)-1
130
+ for i < j {
131
+ t := min(height[i], height[j]) * (j - i)
132
+ ans = max(ans, t)
133
+ if height[i] < height[j] {
134
+ i++
135
+ } else {
136
+ j--
137
+ }
138
+ }
139
+ return
139
140
}
140
141
141
- func min (a, b int) int {
142
- if a > b {
143
- return b
144
- }
145
- return a
142
+ func max (a, b int) int {
143
+ if a > b {
144
+ return a
145
+ }
146
+ return b
146
147
}
147
148
148
- func max (a, b int) int {
149
- if a > b {
150
- return a
151
- }
152
- return b
149
+ func min (a, b int) int {
150
+ if a < b {
151
+ return a
152
+ }
153
+ return b
153
154
}
154
155
```
155
156
@@ -161,34 +162,60 @@ func max(a, b int) int {
161
162
* @return {number}
162
163
*/
163
164
var maxArea = function (height ) {
164
- let i = 0 ,
165
- j = height .length - 1 ;
166
- let res = 0 ;
165
+ let i = 0 ;
166
+ let j = height .length - 1 ;
167
+ let ans = 0 ;
167
168
while (i < j) {
168
- const t = (j - i) * Math .min (height[i], height[j]);
169
- res = Math .max (res, t);
170
- if (height[i] < height[j]) ++ i;
171
- else -- j;
169
+ const t = Math .min (height[i], height[j]) * (j - i);
170
+ ans = Math .max (ans, t);
171
+ if (height[i] < height[j]) {
172
+ ++ i;
173
+ } else {
174
+ -- j;
175
+ }
172
176
}
173
- return res ;
177
+ return ans ;
174
178
};
175
179
```
176
180
177
181
### ** TypeScript**
178
182
179
183
``` ts
180
184
function maxArea(height : number []): number {
181
- const n = height .length ;
182
- let res = 0 ;
183
- for (let i = 0 ; i < n - 1 ; i ++ ) {
184
- for (let j = n - 1 ; j >= 0 ; j -- ) {
185
- if (height [i ] * (j - i ) < res ) {
186
- break ;
185
+ let i = 0 ;
186
+ let j = height .length - 1 ;
187
+ let ans = 0 ;
188
+ while (i < j ) {
189
+ const t = Math .min (height [i ], height [j ]) * (j - i );
190
+ ans = Math .max (ans , t );
191
+ if (height [i ] < height [j ]) {
192
+ ++ i ;
193
+ } else {
194
+ -- j ;
195
+ }
196
+ }
197
+ return ans ;
198
+ }
199
+ ```
200
+
201
+ ### ** C#**
202
+
203
+ ``` cs
204
+ public class Solution {
205
+ public int MaxArea (int [] height ) {
206
+ int i = 0 , j = height .Length - 1 ;
207
+ int ans = 0 ;
208
+ while (i < j ) {
209
+ int t = Math .Min (height [i ], height [j ]) * (j - i );
210
+ ans = Math .Max (ans , t );
211
+ if (height [i ] < height [j ]) {
212
+ ++ i ;
213
+ } else {
214
+ -- j ;
187
215
}
188
- res = Math .max (res , Math .min (height [i ], height [j ]) * (j - i ));
189
216
}
217
+ return ans ;
190
218
}
191
- return res ;
192
219
}
193
220
```
194
221
0 commit comments