🌏 문제
Container With Most Water - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
물을 최대로 채울 수 있게 하는 기둥 두개를 구하는 문제이다.
👽 풀이
첫번째 접근
class Solution:
def maxArea(self, height: List[int]) -> int:
start, end = 0, len(height)-1
while start < end:
curArea = min(height[start], height[end]) * (end-start)
if min(height[start+1],height[end]) * (end-start-1) > curArea:
start += 1
elif min(height[start], height[end - 1]) * (end-1-start) > curArea:
end -=1
else:
return curArea
최종 코드
class Solution:
def maxArea(self, height: List[int]) -> int:
start, end = 0, len(height)-1
maxArea = 0
while start < end:
maxArea = max(maxArea, min(height[start], height[end]) * (end-start))
if height[start] < height[end]:
start += 1
else:
end -= 1
return maxArea
728x90
'💡코딩테스트 > Leetcode' 카테고리의 다른 글
유효한 팰린드롬 | Valid Palindrome (0) | 2023.06.06 |
---|---|
[python|파이썬] 세 수의 합 / 세수의 합이 0이 되는 경우 (0) | 2023.03.15 |
[python] 문자열 뒤집기 / 문자열 반대로 출력하기 (0) | 2023.03.14 |