List<List<Integer>> res = new ArrayList<>(); m = heights.length; if (m == 0) return res; n = heights[0].length; if (n == 0) return res; this.heights = heights; boolean[][] canReachP = newboolean[m][n]; boolean[][] canReachA = newboolean[m][n]; for (int i = 0; i < n; i++) { dfs(0, i, canReachP); dfs(m - 1, i, canReachA); } for (int i = 0; i < m; i++) { dfs(i, 0, canReachP); dfs(i, n - 1, canReachA); } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(canReachA[i][j] && canReachP[i][j]){ List<Integer> temp = new ArrayList<>(); temp.add(i); temp.add(j); res.add(temp); } } } return res;
}
privatevoiddfs(int x, int y, boolean[][] canReach){ canReach[x][y] = true; for (int i = 0; i < 4; i++) { int newX = x + dires[i][0]; int newY = y + dires[i][1]; if (isIn(newX, newY) && heights[x][y] <= heights[newX][newY] && !canReach[newX][newY]) { dfs(newX, newY, canReach); } } }
privatebooleanisIn(int x, int y){ return x >= 0 && x < m && y >= 0 && y < n; } }