For this problem I have taken 2 arrays of size n(same as that of input array). Then I have stored the maximum value of element while tracing the array from left to right in l[n] & from right to left in r[n]. Then I added the min(l[i], r[i]) - a[i] to the rainWater variable.
Time complexity : O(n)
Space Complexity : O(n)
66
For this problem I have taken 2 arrays of size n(same as that of input array). Then I have stored the maximum value of element while tracing the array from left to right in l[n] & from right to left in r[n]. Then I added the min(l[i], r[i]) - a[i] to the rainWater variable. Time complexity : O(n) Space Complexity : O(n)