Why this solution does not work?
This is a solution for 1D array:\n\n\nclass Solution {\n    public int trap(int[] height) {\n        \n        \n        // check if empty (edgecase)\n        i
This is a solution for 1D array:\n\n```\nclass Solution {\n    public int trap(int[] height) {\n        \n        \n        // check if empty (edgecase)\n        if(height.length == 0) return 0;\n        \n        // [4,2,0,3,2,5]\n        // traverse from left to right\n          // add max till now to the left array\n        int[] left = new int[height.length]; // [4,4,4,4,4,5]\n        left[0] = height[0];\n        for(int i=1;i<height.length;i++) {\n            left[i] = Math.max(left[i-1], height[i]);\n        }\n        \n        // traverse from right to left\n          // add max till now to the right array\n        \n        int[] right = new int[height.length]; // [5,5,5,5,5,5]\n        right[height.length-1] = height[height.length-1];\n        for(int i=height.length-2;i>=0;i--) {\n            right[i] = Math.max(right[i+1], height[i]);\n        }\n        \n        // traverse for length of height\n            // max(left[i], right[i]) - height[i] , add this total\n        \n        int total = 0; // 0 + 2 + 4 + 1 + 2 + 0\n        for(int i=0;i<height.length;i++) {\n            total += Math.min(left[i], right[i]) - height[i];\n        }\n        \n        return total;\n    }\n}\n```\n\nThis is a similar solution for 2D array I wrote. I\'m calulating 4 arrays for a matrix. And then for a particular cell I\'m calculating the water it can trap.:\n```\nclass Solution {\n    public int trapRainWater(int[][] heightMap) {\n        \n        // edge cases\n        \n        int row = heightMap.length;\n        int col = heightMap[0].length;\n        \n        int left[][] = new int[row][col];\n        int right[][] = new int[row][col];\n        int bottom[][] = new int[row][col];\n        int top[][] = new int[row][col];\n        \n        \n        for(int i=0;i<row;i++) {\n            left[i][0] = heightMap[i][0];\n            for(int j=1;j<col;j++) {\n                left[i][j] = Math.max(heightMap[i][j], left[i][j-1]);\n            }\n        }\n        \n        for(int i=0;i<row;i++) {\n            right[i][col-1] = heightMap[i][col-1];\n            for(int j=col-2;j>=0;j--) {\n                right[i][j] = Math.max(heightMap[i][j], right[i][j+1]);\n            }\n        }\n        \n        for(int i=0;i<col;i++) {\n            bottom[0][i] = heightMap[0][i];\n            for(int j=1;j<row;j++) {\n                bottom[j][i] = Math.max(heightMap[j][i], bottom[j-1][i]);\n            }\n        }\n        \n        for(int i=0;i<col;i++) {\n            top[row-1][i] = heightMap[row-1][i];\n            for(int j=row-2;j>=0;j--) {\n                top[j][i] = Math.max(heightMap[j][i], top[j+1][i]);\n            }\n        }\n        \n        \n        int res = 0;\n        \n        for(int i=0;i<row;i++) {\n            for(int j=0;j<col;j++) {\n                res += Math.min(left[i][j], Math.min(right[i][j], Math.min(top[i][j], bottom[i][j]))) - heightMap[i][j];\n            }\n        }\n        \n        return res;\n        \n    }\n}\n```\n\nWhy does this solution not working? I tried checking all other solutions for this problem is using BFS Heap.
[]