Scribbling

LeetCode: 11. Container With Most Water 본문

Computer Science/Coding Test

LeetCode: 11. Container With Most Water

focalpoint 2021. 8. 17. 20:52

Greedy하게...

 

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_Area = 0
        l = 0
        r = len(height) - 1
        while l < r:
            w = r - l
            h = min(height[l], height[r])
            max_Area = max(max_Area,w*h)
            if height[l] < height[r]:
                for next_l in range(l+1, len(height)):
                    if height[next_l] > height[l]:
                        l = next_l
                        break
                if next_l == len(height) - 1:
                    break
            else:
                for next_r in range(r-1, -1, -1):
                    if height[next_r] > height[r]:
                        r = next_r
                        break
                if next_r == 0:
                    break
        return max_Area

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 16. 3Sum Closest  (0) 2021.08.21
LeetCode: 15. 3Sum  (0) 2021.08.19
LeetCode: 8. String to Integer (atoi)  (0) 2021.08.16
LeetCode: 6. ZigZag Conversion  (0) 2021.08.16
LeetCode: 207. Course Schedule  (0) 2021.08.14