[JAVA] beats 98%  with comments and complexity analysis
time complexity:\nnumber of persons: n\nnumber of restrictions: m\nnumber of requests: k\n\ninitialize friends and enemies: O(n + m)\nCollaping find: O(n)\nmerg
time complexity:\nnumber of persons: n\nnumber of restrictions: m\nnumber of requests: k\n\ninitialize friends and enemies: O(n + m)\nCollaping find: O(n)\nmerge enemies set: O(m)\n\nin total: n + m + k * (n + m) = O (k * (n + m))\n\n\nSpace: O(n + m)\n\n```\nclass Solution {\n    public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {\n        boolean[] res = new boolean[requests.length];\n        \n        Set<Integer>[] enemies = new Set[n];\n        int[] friends = new int[n];\n        \n        for (int i = 0; i < n; i++) {\n            friends[i] = i;\n            enemies[i] = new HashSet<>();\n        }\n        \n        for (int i = 0; i < restrictions.length; i++) {\n            //add person A to person B\'s enmey circle.\n            //add person B to person A\'s enemy circle.\n            enemies[restrictions[i][0]].add(restrictions[i][1]);\n            enemies[restrictions[i][1]].add(restrictions[i][0]);\n        }\n        \n        for (int i = 0; i < requests.length; i++) {\n            int personA = findRootFriend(friends, requests[i][0]);\n            int personB = findRootFriend(friends, requests[i][1]);\n            Set<Integer> personAEnemies = enemies[personA];\n            Set<Integer> personBEnemies = enemies[personB]; \n            \n            if (personA == personB) {\n                res[i] = true;\n            } else if (!personAEnemies.contains(personB) && !personBEnemies.contains(personA)) {\n                //can merge. update personB\'s root to be person A\n                friends[personB] = personA;\n                \n                // add all B\'s enemies to A\'s enemyies\n                personAEnemies.addAll(personBEnemies);\n                \n                // inform all B\'s enemies that A is a new enemy\n                for (int k : personBEnemies) {\n                    enemies[k].add(personA);\n                }\n                \n                res[i] = true;\n            } else {\n                res[i] = false;\n            }\n        } \n        \n        return res;\n    }\n    \n    private int findRootFriend(int[] friends, int i) {\n        if (friends[i] == i) {\n            return i;\n        }\n        \n        return findRootFriend(friends, friends[i]);\n    }\n}\n```
[]