Skip to content

Commit 4fd8ec1

Browse files
authored
Update 11-container-with-most-water.js
1 parent 47d626c commit 4fd8ec1

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

11-container-with-most-water.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
* @return {number}
44
*/
55
const maxArea = function(height) {
6-
const arr = [];
7-
const arr_len = height.length;
8-
for (let i = 0, j = arr_len - 1; i < j; ) {
9-
arr.push(Math.abs(j - i) * Math.min(height[i], height[j]));
10-
if (height[i] < height[j]) {
11-
i++;
12-
} else {
13-
j--;
14-
}
6+
let res = 0, l = 0, r = height.length - 1
7+
while(l < r) {
8+
const tmp = (r - l) * Math.min(height[l], height[r])
9+
if(tmp > res) res = tmp
10+
if(height[l] < height[r]) l++
11+
else r--
1512
}
16-
17-
return Math.max.apply(Math, arr);
13+
return res
1814
};

0 commit comments

Comments
 (0)