Java| Need help. Tried to reuse Trapping Rain Water I (2 pointers) Expected T:O(MN) S:(MN))
Algorithm\n\n1. Go over each row using https://leetcode.com/problems/trapping-rain-water/ solution(2 pointers), calc and store water that can be trapped in maxP
**Algorithm**\n\n1. Go over each row using https://leetcode.com/problems/trapping-rain-water/ solution(2 pointers), calc and store water that can be trapped in maxPossible array\n2. Go over each column just as above with 2 pointer approach, store water that can be trapped horizontally.\n3. For each i,j -> Take the min of water that can be trapped horizontally, vertically and add it to result count\n\n**Last failed test**\n\nInput:\n[[12,13,1,12],[13,4,13,12],[13,8,10,12],[12,13,12,12],[13,13,13,13]]\nOutput:\n15\nExpected:\n14\n\nWould appreciate the flaw in logic as well as why it fails for this test case. I tried manually and get 15 as my algo would\n\n```\nclass Solution {\n    \n    int rows;\n    int cols;\n    public int trapRainWater(int[][] heightMap) {\n        int waterCount = 0;\n        rows = heightMap.length;\n        cols = heightMap[0].length;\n        int[][] maxPossible = new int[rows][cols];\n        int lo = 0;\n        int hi = cols-1;\n        //Go over all rows one after the other\n        for (int row = 0; row < rows; row++) {\n            lo = 0;\n            hi = cols - 1;\n            int leftMax = heightMap[row][0];\n            int rightMax = heightMap[row][hi];\n            while (lo <= hi) {\n                if (heightMap[row][lo] <= heightMap[row][hi]) {\n                    //Move towards hi\n                     leftMax = Math.max(leftMax, heightMap[row][lo]);\n                    maxPossible[row][lo] = leftMax - heightMap[row][lo];\n                    lo++;\n                } else {\n                    //Move down\n                    rightMax = Math.max(rightMax, heightMap[row][hi]);\n                     maxPossible[row][hi] = rightMax - heightMap[row][hi];\n                     hi--;\n                }\n            } \n        }\n        \n        //Go over all cols one after the other\n        for (int col = 0; col < cols; col++) {\n            lo = 0;\n            hi = rows - 1;\n            int leftMax = heightMap[0][col];\n            int rightMax = heightMap[hi][col];\n            while (lo <= hi) {\n                if (heightMap[lo][col] <= heightMap[hi][col]) {\n                    //Move towards hi\n                    leftMax = Math.max(leftMax, heightMap[lo][col]);\n                    waterCount += Math.min(maxPossible[lo][col], leftMax - heightMap[lo][col]);\n                    lo++;\n                } else {\n                    //Move down\n                     rightMax = Math.max(rightMax, heightMap[hi][col]);\n                     waterCount += Math.min(maxPossible[hi][col], rightMax - heightMap[hi][col]);\n                     hi--;\n                }\n            } \n        }\n        \n        return waterCount;\n    }\n}\n```
['Java']