[Leetcode]42. Trapping Rain Water(x)

2024. 8. 1. 18:32Algorithm

1. problem : 

https://leetcode.com/problems/trapping-rain-water/description/

 

2. solution 1 :

class Solution:
    def trap(self, height: List[int]) -> int:
        if not height:
            return 0
    
        n = len(height)
        left_max = [0] * n
        right_max = [0] * n
        
        left_max[0] = height[0]
        for i in range(1, n):
            left_max[i] = max(left_max[i - 1], height[i])
        
        right_max[n - 1] = height[n - 1]
        for i in range(n - 2, -1, -1):
            right_max[i] = max(right_max[i + 1], height[i])
        
        water_trapped = 0
        for i in range(n):
            water_trapped += min(left_max[i], right_max[i]) - height[i]
        
        return water_trapped

좀 더 이해필요 

 

'Algorithm' 카테고리의 다른 글

[Leetcode]155. Min Stack  (0) 2024.08.01
[Leetcode]20. Valid Parentheses  (0) 2024.08.01
[Leetcode]167. Two Sum II - Input Array Is Sorted  (0) 2024.07.31
[Leetcode]125. Valid Palindrome  (0) 2024.07.31
[Leetcode]128. Longest Consecutive Sequence  (0) 2024.07.31