Skip to content

Commit ff65ed3

Browse files
committed
add container with most water solution
1 parent abc923a commit ff65ed3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ์ •์ˆ˜ ๋ฐฐ์—ด height๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋ฌผ์„ ์ตœ๋Œ€ํ•œ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ๋ผ์ธ์„ ์ฐพ์œผ์„ธ์š”.
3+
ํžŒํŠธ1. ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ์ฐพ์œผ์„ธ์š”.
4+
*/
5+
class Solution {
6+
7+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
8+
public int maxArea(int[] height) {
9+
10+
int max = 0;
11+
int start = 0;
12+
int end = height.length - 1;
13+
14+
while (start < end) {
15+
16+
int area = Math.min(height[start], height[end]) * (end - start);
17+
max = Math.max(area, max);
18+
19+
if (height[start] < height[end]) {
20+
start++;
21+
} else {
22+
end--;
23+
}
24+
}
25+
26+
return max;
27+
}
28+
}
29+

0 commit comments

Comments
ย (0)