Skip to content

Commit 870556d

Browse files
committed
feat: week6 - container with most water
1 parent 95d9c8a commit 870556d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
이중 For-loop을 사용하면 시간복잡도가 O(n^2)이 되므로 시간초과가 발생
3+
리트코드의 Hint를 참고해 Two Pointer를 사용하는 방식으로 시도했습니다.
4+
5+
Time Complexity: O(n)
6+
- While 루프를 사용해서 인덱스 i와 j가 만날 때까지 반복으로 진행하기에 O(n)
7+
8+
Space Complexity: O(1)
9+
- height 값과, max_area 값 모두 상수 공간을 사용
10+
'''
11+
class Solution:
12+
def maxArea(self, height: List[int]) -> int:
13+
i, j = 0, len(height) - 1
14+
max_area = 0
15+
16+
while i < j:
17+
area = min(height[i], height[j]) * (j-i)
18+
max_area = max(area, max_area)
19+
20+
if height[i] < height[j]:
21+
i += 1
22+
else:
23+
j -= 1
24+
25+
return max_area

0 commit comments

Comments
 (0)