반응형

문제 출처 :

 

leetcode.com/problems/container-with-most-water/

 

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

 

 

 

알고리즘 분석 :


문제 해결에 필요한 사항

1. 투포인터

 

left, right를 height의 시작과 끝점으로 두어

 

왼쪽 height가 작다면 left ++

오른쪽 height가 작다면 right --를 하며 최대 넓이를 구하면 정답을 구할 수 있다.

 

 

 

 

 

소스 코드 : 

 

class Solution(object):
    def maxArea(self, height):
        ans = 0
        
        i = 0
        j = len(height) - 1
        while True:
            if i >= j:
                break
            
            ans = max(ans, (j - i) * min(height[i], height[j]))
            
            if height[i] < height[j]:
                i += 1
            else:
                j -= 1
        
        return ans
반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1000번] A+B  (0) 2022.04.18
[2636번] 치즈  (1) 2021.06.01
[35번] Search Insert Position  (0) 2021.05.07
[1018번] 체스판 다시 칠하기  (0) 2020.03.16
[17472번] 다리 만들기 2  (0) 2020.03.14