To find the pool in the matrix, the following algorithm is used. The whole question is as follows:
2-D matrix, where each cell represents the height of the cell. water can flow from a cell with a higher height to the bottom. There is a pool when there is no cell with a lower height in the neighbors (Left, Right, Up, Down, Diagonally). You should find the maximum pool unit size.
I have implemented the code. I am looking for time Complexity. In my opinion, the time complexity is O (n * m), where n and m are the row and column of the matrix. Please check.
public final class Basin { private Basin() {} private static enum Direction { NW(-1, -1), N(0, -1), NE(-1, 1), E(0, 1), SE(1, 1), S(1, 0), SW(1, -1), W(-1, 0); private int rowDelta; private int colDelta; Direction(int rowDelta, int colDelta) { this.rowDelta = rowDelta; this.colDelta = colDelta; } public int getRowDelta() { return rowDelta; } public int getColDelta() { return colDelta; } } private static class BasinCount { private int count; private boolean isBasin; private int item; BasinCount(int count, boolean basin, int item) { this.count = count; this.isBasin = basin; this.item = item; } }; public static BasinData getMaxBasin(int[][] m) { if (m.length == 0) { throw new IllegalArgumentException("The matrix should contain atleast one element."); } final boolean[][] visited = new boolean[m.length][m[0].length]; final List<BasinCount> basinCountList = new ArrayList<>(); for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[0].length; j++) { if (!visited[i][j]) { basinCountList.add(scan(m, visited, i, j, m[i][j], new BasinCount(0, true, m[i][j]))); } } } return getMaxBasin(basinCountList); } private static BasinData getMaxBasin(List<BasinCount> basinCountList) { int maxCount = Integer.MIN_VALUE; int item = 0; for (BasinCount c : basinCountList) { if (c.isBasin) { if (c.count > maxCount) { maxCount = c.count; item = c.item; } } } return new BasinData(item, maxCount); } private static BasinCount scan(int[][] m, boolean[][] visited, int row, int col, int item, BasinCount baseCount) {