Skip to content

Commit be51247

Browse files
committed
Container With Most Water
1 parent 533c70f commit be51247

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
// 높이 μˆœμ„œλŒ€λ‘œ μ •λ ¬ν•΄μ„œ, μ²˜μŒλΆ€ν„° 2κ°œμ”© μž‘μ•„μ„œ λ„ˆλΉ„λ₯Ό κ΅¬ν•˜λŠ” 방법 => n^2
4+
// ν•΄μ„€ 참고함. -> 포인터 2개λ₯Ό λ‘¬μ„œ μ–‘μΈ‘μ—μ„œ μ‹œμž‘ν•΄μ„œ 쀑간에 λ§Œλ‚ λ•ŒκΉŒμ§€ μ΅œλŒ€κ°’μ„ κ΅¬ν•œλ‹€
5+
int s = 0;
6+
int e = height.length - 1;
7+
int result = 0;
8+
9+
while(s < e) {
10+
int h = height[s] < height[e] ? height[s] : height[e];
11+
int w = e - s;
12+
int cur = h * w;
13+
if (cur > result) {
14+
result = cur;
15+
}
16+
17+
if (height[s] < height[e]) {
18+
s++;
19+
} else {
20+
e--;
21+
}
22+
// System.out.println(s + " " + e + " " + cur);
23+
}
24+
return result;
25+
}
26+
}

0 commit comments

Comments
Β (0)