I did this one the initial way, like an extension of Trapping rain water I. you know with row and column boundaries, but it failed in test case 19. That was when I figured out the issue of the boundary. By this time I have already spent one hour attempting the problem, so I am here for the intuition 😁
Company Tags on Telegram & Discord Channel Only❤
Hii, yesterday's and today i had just saw and written the code , :(
I did this one the initial way, like an extension of Trapping rain water I. you know with row and column boundaries, but it failed in test case 19. That was when I figured out the issue of the boundary. By this time I have already spent one hour attempting the problem, so I am here for the intuition 😁
respect++
Mind Boggling!!! Great way to explain intuition....first time I felt this was better than neetcode's explanation
Thank you so much for this explanation. You have explained it so clearly.
Back to back hard ❤❤
Thanks LeetCode
Now i know from where water is leaking
thanks for mentioning the initial approach
i have also tried that but failed
Thank you bhaiya, excellant explanation
if a cursor or something is there it will be easy to focus, difficult to get where are you pointing to..... thank you
clear explanAtion
well i did the code which aryan mentioned first but it only passed 15 test cases
class Solution {
public int trapRainWater(int[][] h) {
int n = h.length, m = h[0].length;
int[][] water = new int[n][m];
for(int i = 0 ; i < n; i++){
int leftMax = h[i][0], rightMax = h[i][m - 1], left = 0, right = m - 1;
water[i][0] = 0; water[i][m - 1] = 0;
while(left < right){
if(h[i][left] < h[i][right]){
if(h[i][left] > leftMax){
leftMax = h[i][left];
}
water[i][left] = leftMax - h[i][left];
left++;
} else {
if(h[i][right] > rightMax){
rightMax = h[i][right];
}
water[i][right] = rightMax - h[i][right];
right--;
}
}
}
int totalWater = 0;
for(int i = 0 ; i < m; i++){
int leftMax = h[0][i], rightMax = h[n - 1][i], left = 0, right = n - 1;
water[0][i] = 0; water[n - 1][i] = 0;
while(left < right){
if(h[left][i] < h[right][i]){
if(h[left][i] > leftMax){
leftMax = h[left][i];
}
totalWater += Math.min(water[left][i], leftMax - h[left][i]);
left++;
} else {
if(h[right][i] > rightMax){
rightMax = h[right][i];
}
totalWater += Math.min(water[right][i], rightMax - h[right][i]);
right--;
}
}
}
return totalWater;
}
}
same with me😅
@@rasanpreetsingh1712 same because it doesn't account for globally. It just consider local minimum boundary and not global.
are u competitive coder?
@@iamnoob7593 naah normal leetcoder who struggles with implementation
nderrated explananation