Skip to content

Commit 2febe1b

Browse files
committed
container with most water
1 parent e432fbf commit 2febe1b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
브루트포스를 통해 모든 경우의 수를 연산하여 찾는 방식
3+
배열 height의 길이 -> N
4+
시간 복잡도 : O(N^2) -> 시간 초과
5+
공간 복잡도 : O(1)
6+
*/
7+
class FailSolution {
8+
public int maxArea(int[] height) {
9+
int max=0;
10+
int area=0;
11+
for(int i=0;i<height.length;i++){
12+
for(int j=i+1;j<height.length;j++){
13+
area= Math.max(area,Math.min(height[j],height[i])*(j-i));
14+
if(area>max){
15+
max=area;
16+
}
17+
}
18+
}
19+
return max;
20+
}
21+
}
22+
23+
/**
24+
투포인터를 활용하여, 낮은 높이의 포인터를 갱신하면서 최대 넓이를 구하는 방식
25+
배열 height의 길이 -> N
26+
시간 복잡도 : O(N)
27+
공간 복잡도 : O(1)
28+
*/
29+
class Solution {
30+
public int maxArea(int[] height) {
31+
int left = 0;
32+
int right = height.length - 1;
33+
int max = 0;
34+
while(left < right) {
35+
int h = Math.min(height[right], height[left]);
36+
int w = right - left;
37+
max = Math.max(max, w * h);
38+
39+
if(h == height[right]) {
40+
right--;
41+
} else {
42+
left++;
43+
}
44+
}
45+
return max;
46+
}
47+
}

0 commit comments

Comments
 (0)