Skip to content

Commit d71b70a

Browse files
committed
2. Container With Most Water
1 parent 5591312 commit d71b70a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

container-with-most-water/sunjae95.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. brute force -> time limited
5+
* 2. two pointer
6+
*
7+
* n: length of height
8+
* time complexity: O(n)
9+
* space complexity: O(1)
10+
*/
11+
var maxArea = function (height) {
12+
let answer = 0;
13+
let start = 0;
14+
let end = height.length - 1;
15+
16+
while (start !== end) {
17+
const w = end - start;
18+
const h = Math.min(height[start], height[end]);
19+
answer = Math.max(answer, w * h);
20+
if (height[start] >= height[end]) {
21+
end--;
22+
} else if (height[start] < height[end]) {
23+
start++;
24+
}
25+
}
26+
27+
return answer;
28+
};

0 commit comments

Comments
 (0)