[Java] Unin Find, try to connect then find violation
This is a regular union find set approach, try to connect two persons in each request, then check if the new connection violates the restrictions array.\n\nclas
This is a regular `union find set` approach, try to connect two persons in each request, then check if the new connection violates the `restrictions` array.\n```\nclass Solution {\n    public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {\n        boolean[] res = new boolean[requests.length];\n        \n        int[] friends = new int[n];\n        \n        for (int i = 0; i < n; i++) {\n            friends[i] = i;\n        }\n        \n        for (int i = 0; i < requests.length; i++) {\n            int rootA = friends[requests[i][0]];\n            int rootB = friends[requests[i][1]];\n            \n            if (rootA == rootB) {\n                res[i] = true;\n                continue;\n            }\n            \n            int[] friendsCopy = tryConnect(friends, rootA, rootB);\n            \n            if (isValid(friendsCopy, restrictions)) {\n                res[i] = true;\n                friends = friendsCopy;\n            } else {\n                res[i] = false;\n            }\n        } \n        \n        return res;\n    }\n    \n    private int[] tryConnect(int[] friends, int rootA, int rootB) {\n        // try on a cloned array rather than in place\n\t\tint n = friends.length;\n        int[] friendsCopy = new int[n];\n        \n        for (int i = 0; i < n; i++) {\n            if(friends[i] == rootA) {\n                friendsCopy[i] = rootB;\n            } else {\n                friendsCopy[i] = friends[i];\n            }\n        }\n        \n        return friendsCopy;\n    }\n    \n    private boolean isValid(int[] friends, int[][] restrictions) {\n        for (int[] restriction: restrictions) {\n            int personA = restriction[0];\n            int personB = restriction[1];\n            if (friends[personA] == friends[personB]) {\n                return false;\n            }\n        }\n        return true;\n    }\n}\n```
[]