Dashboard Temp Share Shortlinks Frames API

HTMLify

Trapping Rain Water
Views: 4 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
    def maxWater(self, arr):
        if not arr:
            return 0
            
        n = len(arr)
        left, right = 0, n - 1
        l_max, r_max = 0, 0
        total_water = 0
        
        while left <= right:
            # We process the side with the smaller boundary
            if arr[left] <= arr[right]:
                if arr[left] >= l_max:
                    l_max = arr[left]
                else:
                    total_water += l_max - arr[left]
                left += 1
            else:
                if arr[right] >= r_max:
                    r_max = arr[right]
                else:
                    total_water += r_max - arr[right]
                right -= 1
                
        return total_water