Skip to content

Commit 899c520

Browse files
committed
container-with-most-water
1 parent 1bb6e1b commit 899c520

File tree

1 file changed

+28
-0
lines changed

1 file changed

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

0 commit comments

Comments
ย (0)