Not Sure why getting WA


  • 0
    N

    I tried the same approach as in 2D water tap problem but here I'm taking highest wall on four sides left, right, down & up.
    17/40 test cases passed.
    Can anyone help me out here. Nothing new in the code, same approach as discussed above.

    class Solution { public: int trapRainWater(vector<vector<int>>& heightMap) {
        int n = heightMap.size();
        int water = 0;
        
        if(n == 0) {
            return 0;
        }
        int m = heightMap[0].size();
        
        int lr[n][m];
        int rr[n][m];
        int cu[m][n];
        int cd[m][n];
        
        for(int i = 0;i < n;i++) {
            lr[i][0] = heightMap[i][0];
            rr[i][m-1] = heightMap[i][m-1];
            for(int j = 1;j < m;j++) {
                lr[i][j] = max(lr[i][j-1],heightMap[i][j]);
            }
            for(int j = m-2;j >= 0;j--) {
                rr[i][j] = max(rr[i][j+1],heightMap[i][j]);
            }
        }
        
        for(int i = 0;i < m;i++) {
            cu[i][0] = heightMap[0][i];
            cd[i][n-1] = heightMap[n-1][i];
            for(int j = 1;j < n;j++) {
                cu[i][j] = max(cu[i][j-1],heightMap[j][i]);
            }
            for(int j = n-2;j >= 0;j--) {
                cd[i][j] = max(cd[i][j+1],heightMap[j][i]);
            }
        }
        
        for(int i = 1;i < n-1;i++) {
            for(int j = 1;j < m-1;j++) {
                water += min(min(lr[i][j],rr[i][j]), min(cu[j][i],cd[j][i])) - heightMap[i][j];
            }
        }
        return water;
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.