fork download
  1. class DSU {
  2. int parent[];
  3. int height[];
  4.  
  5. public DSU(int n) {
  6. parent = new int[n];
  7. height = new int[n];
  8. Arrays.fill(parent , -1);
  9. Arrays.fill(height , 1);
  10. }
  11.  
  12. public int findRoot(int node) {
  13. if(parent[node] == -1)
  14. return node;
  15. return parent[node] = findRoot(parent[node]);
  16. }
  17.  
  18. public boolean union(int node1 , int node2) {
  19. int ra = findRoot(node1);
  20. int rb = findRoot(node2);
  21. if(ra != rb) {
  22. if(height[ra] < height[rb])
  23. parent[ra] = rb;
  24. else if(height[ra] > height[rb])
  25. parent[rb] = ra;
  26. else {
  27. parent[ra] = rb;
  28. height[rb] ++;
  29. }
  30. return true;
  31. }
  32. return false;
  33. }
  34. }
  35.  
  36. class Solution {
  37. public int swimInWater(int[][] grid) {
  38. int n = grid.length;
  39. DSU dsu = new DSU(n * n);
  40. List<Integer> positions = new ArrayList<>();
  41. for(int i = 0 ; i < n ; i ++)
  42. for(int j = 0 ; j < n ; j ++)
  43. positions.add(i * n + j);
  44. Collections.sort(positions , (a , b) -> grid[a / n][a % n] - grid[b / n][b % n]);
  45. int dir[][] = new int[][]{{0 , 1} , {0 , -1} , {-1 , 0} , {1 , 0}};
  46.  
  47. for(int position : positions)
  48. {
  49. int r = position / n;
  50. int c = position % n;
  51.  
  52. for(int i = 0 ; i < dir.length ; i ++)
  53. {
  54. int nr = r + dir[i][0];
  55. int nc = c + dir[i][1];
  56.  
  57. if(nr >= 0 && nc >= 0 && nr < n && nc < n && grid[nr][nc] < grid[r][c])
  58. dsu.union(position , nr * n + nc);
  59. }
  60.  
  61. if(dsu.findRoot(0) == dsu.findRoot((n * n) - 1))
  62. return grid[r][c];
  63. }
  64.  
  65. return -1;
  66. }
  67. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:8: error: cannot find symbol
        Arrays.fill(parent , -1);
        ^
  symbol:   variable Arrays
  location: class DSU
Main.java:9: error: cannot find symbol
        Arrays.fill(height , 1);
        ^
  symbol:   variable Arrays
  location: class DSU
Main.java:40: error: cannot find symbol
        List<Integer> positions = new ArrayList<>();
        ^
  symbol:   class List
  location: class Solution
Main.java:40: error: cannot find symbol
        List<Integer> positions = new ArrayList<>();
                                      ^
  symbol:   class ArrayList
  location: class Solution
Main.java:44: error: cannot find symbol
        Collections.sort(positions , (a , b) -> grid[a / n][a % n] - grid[b / n][b % n]);
        ^
  symbol:   variable Collections
  location: class Solution
5 errors
stdout
Standard output is empty