Approach
Depth-first search
For Number of Distinct Islands II, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 80 lines of Java from the credited upstream file 711.java.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- 7 loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int numDistinctIslands2(int[][] grid) {3 final int m = grid.length;4 final int n = grid[0].length;5 6 Set<List<Pair<Integer, Integer>>> islands = new HashSet<>();7 boolean[][] seen = new boolean[m][n];8 9 for (int i = 0; i < m; ++i)10 for (int j = 0; j < n; ++j) {11 List<Pair<Integer, Integer>> island = new ArrayList<>();12 dfs(grid, i, j, seen, island);13 if (!island.isEmpty())14 islands.add(normalize(island));15 }16 17 return islands.size();18 }19 20 private void dfs(int[][] grid, int i, int j, boolean[][] seen,21 List<Pair<Integer, Integer>> island) {22 if (i < 0 || i == grid.length || j < 0 || j == grid[0].length)23 return;24 if (grid[i][j] == 0 || seen[i][j])25 return;26 27 seen[i][j] = true;28 island.add(new Pair<>(i, j));29 30 dfs(grid, i + 1, j, seen, island);31 dfs(grid, i - 1, j, seen, island);32 dfs(grid, i, j + 1, seen, island);33 dfs(grid, i, j - 1, seen, island);34 }35 36 private List<Pair<Integer, Integer>> normalize(List<Pair<Integer, Integer>> island) {37 38 Pair<Integer, Integer>[][] points = new Pair[8][island.size()];39 40 for (int k = 0; k < island.size(); ++k) {41 final int i = island.get(k).getKey();42 final int j = island.get(k).getValue();43 points[0][k] = new Pair<>(i, j);44 points[1][k] = new Pair<>(i, -j);45 points[2][k] = new Pair<>(-i, j);46 points[3][k] = new Pair<>(-i, -j);47 points[4][k] = new Pair<>(j, i);48 points[5][k] = new Pair<>(j, -i);49 points[6][k] = new Pair<>(-j, i);50 points[7][k] = new Pair<>(-j, -i);51 }52 53 for (Pair<Integer, Integer>[] point : points)54 Arrays.sort(point, Comparator.comparingInt(Pair<Integer, Integer>::getKey)55 .thenComparingInt(Pair<Integer, Integer>::getValue));56 57 58 for (Pair<Integer, Integer>[] p : points) {59 for (int i = 1; i < island.size(); ++i)60 p[i] = new Pair<>(p[i].getKey() - p[0].getKey(), p[i].getValue() - p[0].getValue());61 p[0] = new Pair<>(0, 0);62 }63 64 Arrays.sort(points, new Comparator<Pair<Integer, Integer>[]>() {65 @Override66 public int compare(Pair<Integer, Integer>[] a, Pair<Integer, Integer>[] b) {67 for (int i = 0; i < a.length; ++i) {68 if (a[i].getKey() != b[i].getKey())69 return a[i].getKey() - b[i].getKey();70 if (a[i].getValue() != b[i].getValue())71 return a[i].getValue() - b[i].getValue();72 }73 return 0;74 }75 });76 77 return Arrays.asList(points[0]);78 }79}80