Skip to content

Commit ab5ae5a

Browse files
committed
add: DaleStudy#242 Container With Most Water
1 parent 2c0524b commit ab5ae5a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// 1번째 풀이
2+
function maxArea1(height: number[]): number {
3+
let left = 0;
4+
let right = height.length - 1;
5+
const area: number[] = [];
6+
7+
while (left < right) {
8+
const x = right - left;
9+
const y = Math.min(height[left], height[right]);
10+
area.push(x * y);
11+
12+
if (height[left] < height[right]) {
13+
left++;
14+
} else {
15+
right--;
16+
}
17+
}
18+
19+
return Math.max(...area);
20+
};
21+
22+
// 2번째 풀이
23+
function maxArea2(height: number[]): number {
24+
let left = 0;
25+
let right = height.length - 1;
26+
let max = 0;
27+
28+
while (left < right) {
29+
const x = right - left;
30+
const y = Math.min(height[left], height[right]);
31+
const current = x * y;
32+
max = Math.max(max, current);
33+
34+
if (height[left] < height[right]) {
35+
left++;
36+
} else {
37+
right--;
38+
}
39+
}
40+
41+
return max;
42+
};

0 commit comments

Comments
 (0)